More Node.js answers

by Danny
Published 3 min read

Node.js can read .env files on its own. Use the --env-file flag when starting your app, or call process.loadEnvFile() in code, and drop the dotenv dependency.

The short answer

#

Node.js has built-in support for .env files, so you no longer need the dotenv package for the common case. Pass --env-file when you start the process, and the variables appear on process.env before your code runs.


_10
node --env-file=.env server.js

Why it happens

#

For a long time Node.js only read environment variables from the shell that started it. Loading a .env file meant installing dotenv and calling it as early as possible, before any module that read process.env.

Node.js added the --env-file flag in v20.6.0, and later a programmatic process.loadEnvFile() and a util.parseEnv() helper. Env file support is stable in the current docs, and because Node.js 20 has reached end of life, every supported release line has it. If you are on Node.js 22 or 26, you can remove dotenv from most projects.

The file format is the familiar one: one KEY=value per line, with # for comments and quotes for values that contain spaces.


_10
# .env
_10
DATABASE_URL=postgres://localhost:5432/app
_10
API_KEY=dev-key-123
_10
GREETING="Hello there"

How to fix it

#

Use the --env-file flag

This is the recommended option. The file is read before any of your code runs, so there are no ordering problems with imports:


_10
node --env-file=.env server.js

Put it in your package.json scripts so nobody has to remember it:


_10
{
_10
"scripts": {
_10
"dev": "node --watch --env-file=.env src/server.js",
_10
"start": "node src/server.js"
_10
}
_10
}

You can pass the flag more than once. Files are read in order and later files override earlier ones, which suits a shared file plus local overrides:


_10
node --env-file=.env --env-file=.env.local server.js

Variables that are already set in the real environment win over anything in the files. That is usually what you want: a value set by your hosting platform or CI is not replaced by a stray local file.

Make the file optional with --env-file-if-exists

--env-file throws an error if the file is missing. That is helpful locally but awkward in production, where values normally come from the platform. Use --env-file-if-exists to load the file when it is there and carry on quietly when it is not:


_10
node --env-file-if-exists=.env server.js

Call process.loadEnvFile() in code

When you cannot change the start command, for example inside a script run by another tool, load the file from code. With no argument it reads .env in the current working directory:


_10
process.loadEnvFile();
_10
_10
// or a specific file
_10
process.loadEnvFile('./config/.env.test');
_10
_10
console.log(process.env.DATABASE_URL);

Call it before you import anything that reads process.env at the top level. In ES modules, static imports are hoisted and run first, so put the call in a small entry file and load the rest of the app with a dynamic import() afterwards:


_10
// entry.js
_10
process.loadEnvFile();
_10
await import('./server.js');

Like the flag, process.loadEnvFile() does not overwrite variables that already exist in process.env.

Parse without touching process.env

If you want the values as a plain object, for validation or to pass to a config function, read the file and use util.parseEnv():


_10
import { readFile } from 'node:fs/promises';
_10
import { parseEnv } from 'node:util';
_10
_10
const contents = await readFile('.env', 'utf8');
_10
const config = parseEnv(contents);
_10
_10
console.log(config.API_KEY);

This is also the way to make a file override existing variables on purpose, by assigning the parsed result to process.env yourself.

Common pitfalls

#
  • Expecting a file to override a variable already exported in your shell. Both the flag and process.loadEnvFile() leave existing values alone, so check echo $API_KEY if a value seems stuck.
  • Calling process.loadEnvFile() after a module has already read its config. Static import statements run before the rest of the file, so the module sees undefined.
  • Putting --env-file after the script name. Node.js options must come before the file, otherwise they are passed to your script as arguments: use node --env-file=.env app.js, not node app.js --env-file=.env.
  • Relying on variable expansion or multi-environment features from dotenv extensions. The built-in parser covers the standard format, so test anything unusual before removing the dependency.
  • Committing .env to Git. Add it to .gitignore and commit an .env.example with placeholder values instead.
#

Was this resource helpful?