Does NestJS ConfigModule Load .env in Production by Default?
0 reputation · 28 Jan 2025, 06:35 UTC
Background
In a typical NestJS project, ConfigModule.forRoot({isGlobal:true}) is used to centralise configuration. The module’s default behaviour is to read .env files only when NODE_ENV is not set to production. Consequently, a local dev build that relies on a .env file will work, while a production deployment that omits the file and expects platform‑supplied environment variables will encounter undefined values.
Unresolved Decision
There is no enforced strategy: should a production Docker image bundle a .env file, or should the application rely exclusively on the host’s environment variables? The NestJS documentation does not mandate one approach, leaving the choice to the developer.
Questions
- Does ConfigModule automatically load
.envwhenNODE_ENVequalsproduction? - What configuration options are recommended to ensure required config values are present in a production container that does not expose a
.envfile? - Can I override the default
envFilePathto include a.envin production without settingignoreEnvFile?