Replacements for dotenv
Although dotenv is reliable, it may not be necessary or may lack certain features.
Node.js --env-file / --env-file-if-exists
Built into Node.js (v20.6.0+; v22.9.0 for --env-file-if-exists
). Zero dependencies—perfect for most apps that just need to load a .env
at startup.
--env-file
throws if the file is missing. If the file may be absent, use --env-file-if-exists
.
bash
node --env-file=.env index.js
Also supported by:
Remove dotenv preload:
ts
import 'dotenv/config'
// No import needed when using --env-file
Remove explicit dotenv config:
ts
import dotenv from 'dotenv'
dotenv.config({ path: '.env' })
// No runtime configuration needed
In package.json scripts:
json
{
"scripts": {
"start": "node index.js",
"start": "node --env-file=.env index.js"
}
}