Systems

environment variable · env var

/ EN-vy-ruhn-munt VAIR-ee-uh-bul /

An environment variable is a named value that lives outside your code, in the environment your program runs in — and your program reads it when it starts up. Think of it as a sticky note left for the program: PORT=3000, DATABASE_URL=…, API_KEY=… The code says 'whatever PORT is set to, listen there,' without ever hard-coding the number.

This is how the same code behaves differently in different places. On your laptop DATABASE_URL points at a tiny test database; in production it points at the real one — and not a single line of code changes. You just set the variables differently in each environment.

It's also where secrets belong. API keys and passwords go in environment variables precisely so they stay OUT of your source code — if a secret is baked into a file you commit, it ends up in your git history forever, visible to anyone with the repo. Keep them in the environment, and they never touch the codebase.

$ export API_KEY=sk-secret-123
$ node app.js
# in code:
const key = process.env.API_KEY;

Set it in the shell, read it in code — the secret never lives in a file you'd commit.

Don't commit a .env file — that defeats the whole point. Add it to .gitignore.

Also called
env varenvironmentprocess.envgetenv$path