How to Load Environment Variables in Node.js: dotenv, Runtime Flags, and Beyond
Every Node.js app reads configuration from process.env. The question is how those values get there. For years the answer was the dotenv package. Newer Node.js versions can load a .env file on their own, which changes how many projects should be set up.
This guide covers the main options, when each one fits, and the mistakes that cause trouble in staging and production.
The baseline: process.env
Node exposes environment variables through process.env. Values are strings, so convert them when you need numbers or booleans:
const port = Number(process.env.PORT ?? 3000);
const dbUrl = process.env.DATABASE_URL;
if (!dbUrl) {
throw new Error("DATABASE_URL is not set");
}
Fail early when a required value is missing. A clear startup error is easier to debug than a undefined that surfaces inside a query.
Option 1: dotenv
The dotenv package reads a .env file and copies its values into process.env.
npm install dotenv
import "dotenv/config";
console.log(process.env.DATABASE_URL);
Two details matter:
- By default,
dotenvdoes not overwrite variables that are already set. A value exported in your shell wins over the one in.env. This is usually what you want. - The import has to run before any code reads the variables. Put it first in your entry file.
dotenv works well for local development. It is also a common source of confusion when a .env file gets deployed alongside code, so keep the file out of your build and deploy steps.
Option 2: --env-file (Node.js 20.6 and later)
Recent Node.js releases accept an --env-file flag that loads a file before your script starts:
node --env-file=.env app.js
You can pass the flag more than once to layer files:
node --env-file=.env --env-file=.env.local app.js
This removes the need for a dependency in simple projects. Check the Node.js documentation for your exact version, since flag behavior has changed across releases.
Option 3: process.loadEnvFile()
Node also provides process.loadEnvFile() for loading a file from inside your code. It is useful when you want the load step to be explicit, or when a tool starts your app and you cannot change its command line.
process.loadEnvFile(".env");
Like the flag, it is built in. Make sure the Node version in your development and production images supports it before you rely on it.
Option 4: Set variables where the app runs
In production, the platform usually provides variables directly: a container runtime, a process manager, or the hosting provider's dashboard. Your code should not care which source it came from. Reading process.env works the same in every case.
This is the model to aim for. Local development can use a file. Staging and production should receive values from a store that is encrypted, scoped by environment, and controlled by role.
Choosing one
| Situation | Good fit |
|---|---|
| Solo project, recent Node | --env-file in your start script |
Existing project already using dotenv |
Keep it |
| Tool controls how Node starts | process.loadEnvFile() |
| Production | Variables injected by the platform |
Common mistakes
- Committing
.env. Add it to.gitignoreand commit a.env.examplewith placeholder values instead. - Reading values at import time before loading the file. Load configuration first, then import modules that depend on it.
- Mixing up environments. Keep
.env.development,.env.staging, and production values separate, so a local file never points at a production database. - Sharing values in chat. If a teammate needs a value, give them access to the store rather than pasting it into a DM.
A minimal setup
.env.example # committed, placeholder values
.env # local only, ignored by git
{
"scripts": {
"dev": "node --env-file=.env --watch src/server.js"
}
}
Pick the simplest loader that matches your Node version, validate required variables at startup, and keep real values out of the repo. Configuration should be boring. The less time you spend on it, the more time you spend on the code that matters.
