Skip to content

Node.js Environment Variables and Secrets: process.env, --env-file, .env and .gitignore

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Your API will soon need things that must not live in the code: the port your host gives you, a database password, the key for an email or payment service. Hard coding them means every copy of the code, every screenshot and every Git commit carries your secrets. The standard answer is environment variables, settings that the operating system hands to the process when it starts. This lesson shows how Node.js reads them, how to load a local .env file with a built in flag instead of a package, a real bug caused by the fact that every value is text, a config file that refuses to start when a key is missing, and how Git keeps .env out of your repository. Recorded with Node.js 22.23.4 and Git.

Quick answer

Read settings with process.env.NAME; every value is a string or undefined. Locally, put them in a .env file and start with node --env-file=.env app.js, built into Node.js since version 20.6, or --env-file-if-exists when the file is optional. Convert types yourself (Number(...), === "true"), stop at startup when a required secret is missing, add .env to .gitignore, and commit a .env.example with empty values instead. On a server, set the variables in your host dashboard.

35 min read and practice · Content checked on 28 November 2026

In this lesson you will

  • Read environment variables and convert them to the right type
  • Load a .env file with --env-file and no extra package
  • Fail fast when a required secret is missing
  • Keep secrets out of Git and respond correctly to a leak

process.env and the .env file

process.env is an object with every environment variable the process received. Run env.js with nothing set and PORT is undefined. Put PORT=4000 before the command, the way Linux and macOS shells pass a variable to one command, and the same code prints 4000. Note the type: string. Environment variables are always text, so process.env.PORT + 1 gives "40001", not 4001. On Windows PowerShell the syntax is different ($env:PORT=4000), which is one reason teams prefer a file.

A .env file lists NAME=value lines, with # comments and quotes around values that contain spaces. Node.js reads it itself with --env-file=.env, so the old dotenv package is no longer needed for this. If the file is missing, Node.js stops with node: .env.local: not found and exit code 9 before your code runs. --env-file-if-exists prints a note and carries on, which suits a file that only exists on developer machines. Inside code, process.loadEnvFile(".env") does the same job.

A variable that is already set in the real environment wins over the same name in .env. That is what you want: on the server the host sets the true values, and a stray .env that slipped onto it cannot override them. Locally, it means a variable you exported in your shell an hour ago can hide the value in the file, so when a setting seems stuck, print it.

Example: Reading PORT and SHOP_NAME four ways
env.js
console.log("PORT:", process.env.PORT);
console.log("SHOP_NAME:", process.env.SHOP_NAME);
console.log("typeof PORT:", typeof process.env.PORT);
.env
# Local settings. Never commit this file.
PORT=4000
SHOP_NAME="Al Seef Spices"
MAIL_API_KEY=demo-key-123
DEBUG=false
load.js
process.loadEnvFile(".env");
console.log("Loaded shop:", process.env.SHOP_NAME);
Real output: terminal (bash on Linux), five runs
$ node env.js
PORT: undefined
SHOP_NAME: undefined
typeof PORT: undefined
$ PORT=4000 SHOP_NAME="Al Seef Spices" node env.js
PORT: 4000
SHOP_NAME: Al Seef Spices
typeof PORT: string
$ node --env-file=.env env.js
PORT: 4000
SHOP_NAME: Al Seef Spices
typeof PORT: string
$ node --env-file=.env.local env.js
node: .env.local: not found
$ node --env-file-if-exists=.env.local env.js
.env.local not found. Continuing without it.
PORT: undefined
SHOP_NAME: undefined
typeof PORT: undefined
Real output: node load.js
Loaded shop: Al Seef Spices

The run with the missing .env.local ended with exit code 9, the code Node.js uses for an invalid argument. MAIL_API_KEY=demo-key-123 is a made up value for this lesson, never a real key.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

Do I still need the dotenv package?

Not for loading a .env file in current Node.js: --env-file and process.loadEnvFile() are built in. Some older projects and tools still use dotenv, and it is fine to keep it there, but new projects can skip the dependency.

Where do I set environment variables on a real server?

In your host settings: most platforms have an environment or secrets page per project. On your own VPS, use the service manager (a systemd unit or a Docker Compose env_file) with a file only the app user can read.

Are environment variables encrypted?

No. They are plain text inside the running process and in the .env file. They keep secrets out of your code and repository, which is the main goal. For bigger systems, a secrets manager adds encryption at rest, access rules and rotation.

What should go in .env.example?

Every variable the app reads, with empty values for secrets and safe defaults for the rest, plus a short comment where the meaning is not obvious. It is committed to Git, so it must never contain a real key.

How often should I change API keys?

Immediately after any leak or when someone with access leaves, and on a regular schedule your provider or company policy sets. Keys designed for one job, with the narrowest permissions the provider offers, limit the damage if one escapes.

Sources

Facts in this lesson were checked against these official pages on 28 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.