Search code examples
nestjs

NestJs - Fallback at configuration file for ConfigService doesn't work


I'm using node18 and nestjs 9.3.12.

I have such configuration file:

export default () => ({
  AUTH_HOST: process.env.AUTH_HOST || "www.myhost.com"
});

at app.module I have this:

ConfigModule.forRoot({
  isGlobal: true,
  load: [configuration],
});

in my environment AUTH_HOST such record exists, it is empty:

AUTH_HOST=

I have a service that uses the ConfigService

class RootService {
  constructor(private readonly configService: ConfigService) {
    const authHost = this.configService.get<string>('AUTH_HOST');
  }
  // ...
}

so I'm expecting code process.env.AUTH_HOST || "www.myhost.com" will return "www.myhost.com" but it returns empty string. Changing flags of ConfigModule to ignoreEnvFile: true and ignoreEnvVars: true has no effect.

What is correct nestjs way to make a fallback to what I provide at configuration file?


Solution

  • Try these one

    const authHost = this.configService.get<string>('AUTH_HOST','www.myhost.com')