Installing a package just to read .env? Use node --env-file
Node.js reads your variables file itself with the --env-file flag, no package needed. But a value already in the environment wins over the file, and a missing file stops the start.

One line at the top of the entry file reads the variables file, and a whole package sits in the dependencies just for that line: you see this in many Node.js projects, and Node.js itself now does the job. By the end of this article you will be able to start your project with its variables file and no package, and you will know which value wins when the same variable is in two places.
What is the --env-file flag?
The --env-file flag is an option of the node command that reads a variables file before running your code and puts its values in process.env. A variables file is a text file with one variable name and its value on each line, like PORT=3000.
node --env-file=.env app.jsAccording to the Node.js command-line options documentation, the flag was added in Node.js 20.6.0, and it is no longer experimental as of Node.js 24.10.0 and Node.js 22.21.0. Support for values over several lines came in Node.js 21.7.0 and Node.js 20.12.0.
What does the file accept?
The variables file accepts one line per variable, with = between the name and the value, and the same documentation explains the rest:
# database settings
PORT=3000 # this part is a comment
DB_NAME="shop"
export APP_MODE=productionAnything after
#is a comment, even in the middle of a line.Quotes around a value are removed, so
"shop"arrives asshop.Node.js ignores the
exportkeyword before a name, and the value arrives as it is.A value in quotes can continue over several lines, in the versions above.
Which wins: the environment or the file?
The environment wins: if the same variable is in the environment and in the file, Node.js takes the value from the environment. So the file never writes over something that already exists.
PORT=8080 node --env-file=.env app.jsWith this command process.env.PORT is 8080 from the environment, not 3000 from the file.
This helps you on the server: the hosting platform puts the real variables in the environment, and a development file cannot cover them if it ships by mistake. But it confuses you locally when an old variable you exported in the terminal is still there: you edit the file and the value does not change.
How do you load several files in a row?
You load several files by repeating the flag, and a later file wins over an earlier one for the same variable. That way you keep shared values in one file, and the values for your own machine in another:
node --env-file=.env --env-file=.env.local app.jsA value in the environment still sits above both, and the second file sits above the first.
What happens if the file does not exist?
If the file you named does not exist, Node.js throws an error and does not run your code. That is good when the file must be there, because you find out right away instead of the app running without settings.
And if the file is optional (present on your machine and missing on the server), use --env-file-if-exists, which was added in Node.js 22.9.0 and works like the first flag but without an error when the file is missing:
node --env-file-if-exists=.env.local app.jsHow do you move your project off the package?
You move your project by putting the flag in your start script, and removing the package line from your code once you confirm the values arrive as before.
Check your version with
node --version, and make sure it is one of the versions that have the flag.Change the start script in
package.jsontonode --env-file=.env app.js.Print one variable that is not secret at startup, and make sure its value matches the file.
Remove the package line and its import, then remove it from the dependencies.
Run your tests with the same flag (see Node.js tests with node:test).
The flag changes nothing about the safety of the file itself: the variables file stays out of the repository, a secret key never reaches the browser (details in the NEXT_PUBLIC variables article), and it never gets printed in your logs (see logs and sensitive data).
Where do you start today?
Open your project's start script and check whether a package is there only to read the variables file, because one flag on the node command does the same job. Try the flag on a new branch and print one variable. Did the value arrive as it is in the file?
FAQ
Do I have to remove the package I use to read the file now?
No. If your Node.js version has the flag, you can try it in your start script and remove the package line once you confirm the same values arrive.
Does Node.js read the .env file on its own if I do not pass the flag?
No. Node.js reads only the file you name with the flag, so if you drop the flag from the start command, the variables do not arrive.
Can I write a value over several lines, like a private key?
Yes. Quoted values over several lines are supported from certain versions, so check your version before you rely on it.


