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.
_10node --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_10DATABASE_URL=postgres://localhost:5432/app_10API_KEY=dev-key-123_10GREETING="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:
_10node --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:
_10node --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:
_10node --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:
_10process.loadEnvFile();_10_10// or a specific file_10process.loadEnvFile('./config/.env.test');_10_10console.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_10process.loadEnvFile();_10await 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():
_10import { readFile } from 'node:fs/promises';_10import { parseEnv } from 'node:util';_10_10const contents = await readFile('.env', 'utf8');_10const config = parseEnv(contents);_10_10console.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 checkecho $API_KEYif a value seems stuck. - Calling
process.loadEnvFile()after a module has already read its config. Staticimportstatements run before the rest of the file, so the module seesundefined. - Putting
--env-fileafter the script name. Node.js options must come before the file, otherwise they are passed to your script as arguments: usenode --env-file=.env app.js, notnode app.js --env-file=.env. - Relying on variable expansion or multi-environment features from
dotenvextensions. The built-in parser covers the standard format, so test anything unusual before removing the dependency. - Committing
.envto Git. Add it to.gitignoreand commit an.env.examplewith placeholder values instead.
Related reading
#Was this resource helpful?