Environment variables and config
The same code runs on your laptop, in CI and in production, and each needs a different database and different keys. Here is how to keep those values out of the code, and the secrets out of git.
01 One codebase, many places to run
A value that changes between places your code runs is config: the database URL, the port, an API key, the log level. Written into the source, it forces a code change per environment and puts every secret in the repository, where everyone with read access, and every clone, has it.
const db = new Pool({connectionString: 'postgres://admin:hunter2@db.prod/shop',});
const db = new Pool({connectionString: process.env.DATABASE_URL,});
The code stays the same everywhere; each place supplies its own values. The widely used Twelve-Factor App guidelines put it simply: store config in the environment.
02 What an environment variable is
Every process on Linux, macOS and Windows carries a list of NAME=value strings. When one process starts another, the child gets a copy of the parent’s list, plus anything set for that command. In Node the list is process.env.
- set for one command
- read by the app
PORT=4000 node server.js adds PORT to the child only. Changes made inside Node never flow back to the shell, and changes in the shell after start never reach the running process.
03 Every value is a string
The operating system stores only text. process.env.PORT is "4000", not 4000, and a variable that was never set is undefined.
DEBUG=false turns debugging on if you write if (process.env.DEBUG): "false" is a non-empty string, so it is truthy. Compare with the exact text, process.env.DEBUG === 'true'.
04 Read it once, check it at start
Scattering process.env.X through the code hides which settings the app needs, and a missing one surfaces only when that line runs, perhaps hours later. Read everything in one module, convert the types, and refuse to start if something required is missing.
Crashing at start is the friendly outcome: the deploy fails at once, with the variable’s name in the log, instead of the app half-working.
05 A .env file for your own machine
Typing every variable before each command gets old. On your laptop, put them in a .env file. Since Node 20.6, node --env-file=.env loads it before your code runs; on older versions the dotenv package does the same job.
The second run shows the rule: a variable already set in the real environment wins over the file. That is what lets a server or CI override your defaults. dotenv behaves the same way unless you ask it to override.
Keep the real file out of git, and commit a copy with the names but no secret values, so the next person knows what to fill in:
06 Where each environment gets its values
Only your laptop should read a .env file. Everywhere else, the thing that starts your process sets the variables, and secrets come from a store built for them.
| Environment | Settings come from | Secrets come from |
|---|---|---|
| Your laptop | A git-ignored .env file | The same file, with dev-only credentials |
| CI | Variables in the pipeline config | The CI system’s secret settings, masked in logs |
| Production container | docker run -e, or the orchestrator’s config | A secret manager (for example Kubernetes Secrets or a cloud secret store), injected at start |
PORT, LOG_LEVEL, timeouts. Safe to commit as defaults and override per environment.07 When it goes wrong
git add .envA committed secretDeleting the file in the next commit leaves it in history and in every clone. Rotate the secret: issue a new one and revoke the old.console.log(process.env)Secrets in the logsDumping the environment, or a config object, while debugging copies every secret into your log system. Log the names, not the values.ENV DB_PASSWORD=hunter2Secrets baked into an imageA Dockerfile ENV is stored in the image, and anyone who can pull it can read it with docker inspect. Pass secrets when the container starts.VITE_API_KEY=sk_live_…Front-end “env vars” are publicBundlers such as Vite and Next.js copy prefixed variables into the JavaScript sent to every visitor. Only public values belong there.PORT changed, app not restartedExpecting a live updateA process gets its copy once, at start. Change a variable and you must restart the process to see it.