nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.7k stars 7.63k forks source link

Configuration namespaces overwritten in array #11590

Closed chunfenghuayu1 closed 1 year ago

chunfenghuayu1 commented 1 year ago

Is there an existing issue for this?

Current behavior


//config.ts
export default () => ({
    port: 3001
})

//database.ts
export default () => ({
    type: 'mysql',
    host: process.env.SQL_HOST,
    port: process.env.SQL_PORT
})

// app.module.ts
@Module({
    imports: [
        ConfigModule.forRoot({
            envFilePath: ['.env.development', '.env.production'],
            load: [config, database]
        }),
...
    ]
})

if i don't use function registerAs(),the database.port will overwritten the config.port

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-se9i5j?file=src/app.module.ts

Steps to reproduce

No response

Expected behavior

load: [databaseConfig]

1.use a single config,don't need registerAs

2.use a array should constrain configs with namespaces

Package

Other package

@nestjs/config

NestJS version

9.0.0

Packages versions

 "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.3.1",
    "@nestjs/core": "^9.0.0",
    "@nestjs/jwt": "^10.0.3",
    "@nestjs/mapped-types": "*",
    "@nestjs/passport": "^9.0.3",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/swagger": "^6.3.0",
    "@nestjs/typeorm": "^9.0.1",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "helmet": "^6.1.5",
    "mysql2": "^3.2.4",
    "passport": "^0.6.0",
    "passport-jwt": "^4.0.1",
    "passport-local": "^1.0.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "swagger-ui-express": "^4.6.2",
    "typeorm": "^0.3.15"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.5.0",
    "@types/node": "18.15.11",
    "@types/passport-jwt": "^3.0.8",
    "@types/passport-local": "^1.0.35",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "29.5.0",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.5",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.2.0",
    "typescript": "^4.7.4"
  }

Node.js version

18.16.0

In which operating systems have you tested?

Other

No response

Papooch commented 1 year ago

How would you expect this to work? JavaScript cannot read the name of a variable at runtime, so you essentially just passed two unnamed lambdas to the array. The registerAs function gives the config the key under which to nest the namespaced config. Without it, it has no way of knowing where to put it, so it merges it with the root config object.

kamilmysliwiec commented 1 year ago

Please search through some of our old issues on this (this has been discussed several times in the past).