Replacements for dotenv
--env-file / --env-file-if-exists (native, Node.js)
--env-file (Node.js v20.6.0+) and --env-file-if-exists (Node.js v22.9.0+) can be passed as command-line flags to load environment variables from a specified file.
--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.jsAlso supported by:
Remove dotenv preload:
ts
import 'dotenv/config'
// No import needed when using --env-fileRemove explicit dotenv config:
ts
import dotenv from 'dotenv'
dotenv.config({ path: '.env' })
// No runtime configuration neededIn package.json scripts:
json
{
"scripts": {
"start": "node index.js",
"start": "node --env-file=.env index.js"
}
}Node.js parseEnv
parseEnv (Node.js v20.12.0+) can be used to parse a .env string into an object without loading it into process.env.
ts
import { parse } from 'dotenv'
import { parseEnv } from 'node:util'
const envContent = '...'
const env = parse(envContent)
const env = parseEnv(envContent) If the .env is a Node.js Buffer, convert it to a string first with .toString().