Pages
Contents
API
dotenv.load(options?)
Loads .env and optionally .env.{env}. Populates process.env.
| await dotenv.load(); |
| await dotenv.load({ env: 'stage' }); |
| Option | Type | Description |
|---|---|---|
env |
string |
Environment name for .env.{env} β fallback: process.env.NODE_ENV |
override |
boolean |
Overrides existing process.env values (default: false) |
dotenv.get(key, fallback?)
Returns the value of a variable.
| dotenv.get('PORT') // '3000' | null |
| dotenv.get('PORT', '8080') // '3000' or '8080' as fallback |
| dotenv.get('MISSING', '42') // '42' |
dotenv.has(key)
Checks whether a variable is set.
| dotenv.has('PORT') // β true |
| dotenv.has('MISSING') // β false |
dotenv.getAll()
Returns a snapshot of process.env (copy, not a reference).
| dotenv.getAll(); |
| // β { PORT: '3000', NODE_ENV: 'stage', ... } |
dotenv.require(key)
Returns the value or throws an error if the variable is missing.
| dotenv.require('TURN_SECRET') // β 'abc123' or Error |
Useful for critical variables that must be present at startup.
dotenv.optional(key, fallback)
Sets a fallback value if the variable is not set.
| dotenv.optional('LOG_LEVEL', 'info'); |
dotenv.loaded
true after the first successful load() call.
| dotenv.loaded // β true | false |
dotenv.files
Array of the most recently loaded file paths.
| dotenv.files // β ['.env', '.env.stage'] |
Event: `dotenv:loaded`
Emitted after load().
| // Payload: |
| { |
| env: 'stage', |
| files: ['.env', '.env.stage'], |
| override: false |
| } |