I have 2 different file with environment variables: .env.development
and .env.production
.
I am trying to load the correct file through the ConfigModule like so:
app.module.ts
@Module({
imports: [
ConfigModule.forRoot({
envFilePath: `.env.${process.env.NODE_ENV}`,
isGlobal: true,
})
],
})
In package.json
I specify the right NODE_ENV per startup script:
"start:dev": "NODE_ENV=development nest start --watch",
"start:prod": "NODE_ENV=production node dist/main",
My development file contains PORT=2994
and production contains PORT=2997
.
In my main.ts
file I am logging both the PORT and the .env. file path + NODE_ENV of the ConfigService, like so:
const port = configService.get<number>( PORT );
console.log( Running on port: , port);
console.log(`.env.${configService.get( NODE_ENV )}`);
The NODE_ENV file path log is always correct (.env.development for development and .env.production for production). The port however, which is supposed to be retrieved from the correct file is logged 2994 on both development and production.
Only when I remove or empty the .env.development file, the production values are used. It seems as if the .env.development will always take precedence over the .env.production file.
What am I doing wrong here?