Environment Variables, .env and Secrets

2.One Recipe, Many Branches, Private Safes

A

In this chapter

We'll learn why code and settings are kept apart — environment variables, PATH and .env files — and the special care secrets need, explained as one recipe used by many restaurant branches, each with its own settings and its own safe.

14–16 min

The Problem in Real Life

Anna starts the app. It crashes instantly: Error: DATABASE_URL is not defined. A teammate helpfully pastes his whole .env file into the team chat: "Here, just use mine." It contains the database password and the live payment API key: PAYMENT_API_KEY=sk_live_....

She saves it as .env, the app starts, and she runs git add . to commit her setup changes. Just before she presses Enter on the commit, John puts a hand on her keyboard. "Run git status first." The list includes a new file: .env. It was about to go into the repository — and from there, to GitHub.

J

Code is shared with everyone on the team. Secrets are shared with almost no one. Never mix them up.

John

Settings Written Into Code vs. Settings Kept Outside It

Same code, different settings

Your laptop, staging and production run the same code but need different databases and keys.

Some settings are secrets

Passwords and API keys give access to money and data. They must never be in code or Git.

Secrets leak easily

Pasted in chat, committed by accident, pushed to GitHub — once out, a secret must be replaced.

Environment Variables, .env Files and Secrets

The restaurant branches analogy: a restaurant chain uses one recipe book in every branch — the same dishes everywhere. But each branch has its own settings: its address, its local suppliers, its opening hours. And each manager has the combination to their own safe — known to almost nobody. The recipe is the code. The branch settings are the configuration. The safe combination is a secret. Mixing them up — printing the safe combination in the recipe book that every branch gets — would be a disaster.

  • Configuration vs. code: code is the same everywhere: on Anna's laptop, on staging and in production (Act 19). Configuration is the settings that differ between those places: which database to use, which port to listen on, which payment gateway (test or live), whether debug logging is on. Keeping configuration outside the code means the same code can run anywhere — just give it different settings.
  • Secrets vs. configuration: secrets are configuration that must stay private: database passwords, API keys, signing keys. Ordinary configuration (a port number, a feature switch) can be seen by any developer. A secret, if leaked, lets someone read customer data or spend the company's money.
  • Environment variables — the settings a program receives when it starts: an environment variable (Act 06) is a named value the operating system gives to every program it starts. The app reads its configuration from them: process.env.DATABASE_URL in Node.js, os.environ in Python. Every place the app runs sets them differently. They're the standard way to pass configuration — and secrets — into an app.
  • PATH — an environment variable you already know: PATH (Act 06) is the environment variable listing the folders where the shell looks for programs. It's why node: command not found happened in Act 04, and why a freshly installed tool sometimes needs a new terminal.
  • .env file — a sticky note of settings next to the project: on a developer's laptop, typing every environment variable by hand would be painful. So most projects use a .env file: a plain text file in the project folder with one NAME=value per line. A small library loads it into the environment when the app starts. It's for local development — servers get their settings from their hosting platform or a secrets manager (Act 19).
Table — Code vs. configuration vs. secrets
TypeRestaurant versionExampleWhere it lives
CodeThe recipe bookcalculateTotal(), the checkout pageGit
ConfigurationThe branch's settingsPORT=3000, LOG_LEVEL=debugEnvironment variables
SecretThe safe combinationDATABASE_URL with password, PAYMENT_API_KEYPassword/secrets manager → environment variables
Table — Secret safety rules
RuleWhy
.env is in .gitignoreSecrets never enter the repository or its history
Commit .env.example with names onlyNew developers know what to fill in
Never paste secrets in chat, email or AI toolsCopies end up in places you can't control
Use test keys on laptopsA stolen laptop can't charge real customers
Rotate any secret that may have leakedAssume it's compromised; make the old one useless

Same code, different settings

The code (one recipe book)

identical on every machine — in Git

each place supplies its own environment variables

Laptop: .env file

local database, TEST payment key

Staging: platform settings

staging database, TEST key

Production: secrets manager

real database, LIVE key

App reads process.env.DATABASE_URL

behaves correctly wherever it runs

.env.example — committed to Git, no real values
# Copy this file to .env and fill in values from the password manager.
# .env is in .gitignore — never commit it.
PORT=3000
DATABASE_URL=
REDIS_URL=
PAYMENT_API_KEY= # use a TEST key (starts with sk_test_)
SMS_API_KEY=
Reading configuration in code
const port = process.env.PORT ?? 3000;
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
// Fail fast with a clear message instead of a confusing crash later
throw new Error("DATABASE_URL is not set — copy .env.example to .env");
}

Notice the code never contains the actual password — only the name of the variable that holds it.

The golden rules for secrets: never commit .env — it must be in .gitignore (Act 15). Instead, commit a .env.example file with every variable's name but no real values, so a new developer knows what to fill in. Never paste secrets in chat, email, tickets or AI assistants. Get them from the team's password manager or secrets manager. And give developers test keys, not live ones — a laptop should never hold the key that charges real customers.

What happened, and the fix: BlueTicket's .gitignore listed .env.local but not plain .env — a small gap nobody had noticed. Anna adds .env to .gitignore, creates a .env.example with empty values, and replaces her pasted copy with values from the password manager — using the test payment key. Because the live key was pasted into chat, John asks the payment provider to rotate it (issue a new one and switch the old one off), just in case. A secret that has been shared carelessly has to be treated as leaked.

John reminds her of something that will happen in Act 22: "One day someone will push a key to GitHub anyway. When it happens, don't hide it. Rotate the key first, then fix the history."

Key Takeaway

Keep code and configuration apart, like one recipe used by many restaurant branches with their own settings: code is the same everywhere, configuration differs per environment, and secrets are configuration that must stay private. Apps read configuration from environment variables; on a laptop, a .env file supplies them. Never commit .env (commit a .env.example instead), never paste secrets anywhere, use test keys locally, and rotate any secret that might have leaked.

Why This Matters

Leaked secrets are one of the most common and most costly security incidents in software — keys pushed to public GitHub repositories are found by automated bots within minutes. Separating code from configuration is also what lets the same build run safely on a laptop, on staging and in production (Act 19). These habits matter from your very first day.

The app finds its database now. But npm install is still complaining about versions — and Anna's new laptop has a different Node.js version than the project expects. To understand why that matters, she needs to understand how projects get their dependencies.