Full answer
Configuration that changes between environments (ports, database URLs, API keys) should not be written in code. Environment variables are the simplest way to supply it.
Facts about process.env:
- Every value is a string, or undefined when missing. Convert numbers and booleans yourself: Number(process.env.PORT). The text 'false' is a truthy string.
- Setting process.env.X = 5 stores the string '5'.
- Changes affect only the current process and its children.
.env files:
- A .env file has KEY=value lines, with comments starting with # and optional quotes. It is for local development. Never commit it. Commit a .env.example with fake values.
- node --env-file=.env app.js loads it before your code starts. Use --env-file-if-exists for an optional file (Node 22 and later), and repeat the flag for several files, where later files override earlier ones.
- process.loadEnvFile(path) loads a file from code (Node 21.7 and 20.12 and later).
- util.parseEnv(text) returns an object and does not touch process.env. It is useful in tests.
- Before these features most projects used the dotenv package, which is still common.
Production: set variables in the platform (container environment, Kubernetes secrets, a secret manager), not in files inside the image.
NODE_ENV: a convention. Libraries such as Express change behaviour when it equals 'production' (for example caching views, less verbose errors). Node itself ignores it. Set it to 'production' on servers, and do not invent many values such as 'staging' that libraries do not know; use a separate variable for that.
const { parseEnv } = require('node:util');
const { spawnSync } = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const text = `
# local settings
PORT=3000
APP_NAME="Schoolabe API"
DEBUG=false
EMPTY=
`;
// 1. parseEnv only parses. It does not change process.env.
const parsed = parseEnv(text);
console.log('parsed:', parsed);
console.log('process.env.PORT is set by parseEnv:', process.env.PORT !== undefined);
// 2. Values are strings: the usual traps.
console.log('typeof PORT:', typeof parsed.PORT, '| PORT + 1 =', parsed.PORT + 1, '| Number(PORT) + 1 =', Number(parsed.PORT) + 1);
console.log('"false" is truthy:', Boolean(parsed.DEBUG));
// 3. loadEnvFile and --env-file in a child process.
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'env-'));
const file = path.join(dir, '.env');
fs.writeFileSync(file, text);
const viaFlag = spawnSync(process.execPath, ['--env-file=' + file, '-p', 'process.env.APP_NAME'], { encoding: 'utf8' });
console.log('--env-file:', viaFlag.stdout.trim());
const viaCode = spawnSync(process.execPath, ['-e', `process.loadEnvFile(${JSON.stringify(file)}); console.log(process.env.PORT)`], { encoding: 'utf8' });
console.log('process.loadEnvFile:', viaCode.stdout.trim());
// 4. A value that is already in the environment wins over the file.
const override = spawnSync(process.execPath, ['--env-file=' + file, '-p', 'process.env.PORT'], {
encoding: 'utf8',
env: { ...process.env, PORT: '8080' },
});
console.log('real environment wins over the file:', override.stdout.trim());
fs.rmSync(dir, { recursive: true });The child processes show how the flag and loadEnvFile behave, without changing the environment of this script. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): parsed: { APP_NAME: 'Schoolabe API', DEBUG: 'false', EMPTY: '', PORT: '3000' } process.env.PORT is set by parseEnv: false typeof PORT: string | PORT + 1 = 30001 | Number(PORT) + 1 = 3001 "false" is truthy: true --env-file: Schoolabe API process.loadEnvFile: 3000 real environment wins over the file: 8080
Key points
- process.env values are always strings.
- --env-file, loadEnvFile and parseEnv are built in.
- Never commit .env. Commit .env.example.
- Production values come from the platform.
Common mistakes
- Committing a .env file with real secrets.
- Using if (process.env.FLAG) when the value is the string 'false'.
- Assuming process.env.PORT is a number.
- Reading process.env all over the code instead of in one config module.
Follow-ups
- Why read the environment in one place only?
- How do you handle secrets in Kubernetes?
- What does NODE_ENV change in Express?
Tips
- Mention that all values are strings in the first sentence.
- Know the flag --env-file and the function util.parseEnv.
- Say .env is for development, the platform is for production.