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.
console.log("PORT:", process.env.PORT);
console.log("SHOP_NAME:", process.env.SHOP_NAME);
console.log("typeof PORT:", typeof process.env.PORT);# Local settings. Never commit this file.
PORT=4000
SHOP_NAME="Al Seef Spices"
MAIL_API_KEY=demo-key-123
DEBUG=falseprocess.loadEnvFile(".env");
console.log("Loaded shop:", process.env.SHOP_NAME);$ 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: undefinedLoaded shop: Al Seef SpicesThe 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
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.
- Node.js docs: Command line options (--env-file)
- Node.js docs: Environment variables and .env files
- Node.js learn: Reading environment variables
- Git docs: gitignore
- GitHub docs: About secret scanning
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.