nestjs-config icon indicating copy to clipboard operation
nestjs-config copied to clipboard

configfile not being read

Open stefaanMLB opened this issue 3 years ago • 1 comments

Issue type:

  • [ ] question
  • [X] bug report
  • [ ] feature request
  • [ ] documentation issue

nestjs-config version

  • "nestjs-config": "^1.4.10",

@nestjs/common+core or other package versions

  • "@nestjs/common": "^8.2.5",
  • "@nestjs/core": "^8.0.0",
  • "@nestjs/platform-express": "^8.0.0",
  • "@nestjs/schedule": "^1.0.2",
  • "@nestjs/serve-static": "^2.2.2",

Excepted behavior

settings should get the value defined in the config file,

Actual behavior or outcome (for issue)

all settings get the default values instead

Context

Trying to replace the stock nestjs/config library withnestjs-config to enable config reload at runtime

Replication/Example

// src/config/config.ts (simplified)
export default {
  port: parseInt(process.env?.PORT ?? '3821'),
  timezone: 'Europe/Brussels',
}
// src/app.module.ts (simplified)
import { Global, Module } from '@nestjs/common'
import { DataAccessModule } from './data-access.module'
import { LogModule } from './log.route/log.module'
import { ApplicationController } from './application.route/application.controller'

import * as path from 'path'
import { ConfigModule } from 'nestjs-config'

const configPath = path.resolve(__dirname, 'config', 'config.js')
console.log(configPath)  // this outputs C:\Users\lcartreul\Documents\projecten\log-server\dist\src\config\config.js which is correct

@Global()
@Module({
  imports: [DataAccessModule.register(), LogModule, ConfigModule.load(configPath)],
  controllers: [ApplicationController],
  providers: [],
  exports: [DataAccessModule],
})
export class AppModule {}
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
import { ConfigService } from 'nestjs-config'

async function bootstrap() {
  const app = await NestFactory.create(AppModule)
  const config = app.get<ConfigService>(ConfigService)
  const port = config.get('port', 3822)

  await app.listen(port)
  console.log(`Listening on port ${port}`)
  // Here `3822` is returned instead of the expected `3821`
}
bootstrap()

stefaanMLB avatar Feb 07 '22 10:02 stefaanMLB

same here, how is this still happening

edit:

I figured it out

try this inside your bootstrap():

const app = await NestFactory.create(AppModule);
await app.init();

const config = app.get(ConfigService);
await app.listen(config.port || 5000);

amingst avatar Jun 21 '22 22:06 amingst