Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configfile not being read #525

Open
1 of 4 tasks
stefaanMLB opened this issue Feb 7, 2022 · 1 comment
Open
1 of 4 tasks

configfile not being read #525

stefaanMLB opened this issue Feb 7, 2022 · 1 comment

Comments

@stefaanMLB
Copy link

Issue type:

  • question
  • 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()
@amingst
Copy link

amingst commented Jun 21, 2022

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants