dotenv-defaults
Give Node.js applications predictable fallbacks with a separate .env.defaults file for shared configuration.
Set up dotenv-defaults
Install the package in your Node.js application.
npm install dotenv-defaults
Add shared, non-secret fallback values to .env.defaults.
PORT=3000
LOG_LEVEL=info
Set environment-specific values in your local .env file. For example, this file overrides the fallback port.
PORT=8080
Load configuration before the application reads process.env. This example uses an ES module entry point.
import 'dotenv-defaults/config'
console.log(process.env.PORT)
console.log(process.env.LOG_LEVEL)
With neither variable set in the running process, the example prints 8080 and info.
Which value takes precedence?
- Values already set in
process.envare kept by default. - Values in
.envfill in variables missing from the process. - Values in
.env.defaultssupply any remaining fallbacks.
Commit .env.defaults when it contains only non-secret defaults. Keep .env outside version control when it contains local settings or credentials.
For build-time configuration in webpack, see the dotenv-webpack setup guide. The original write-up on defaults in process.env explains the motivation for the package.