Local, Staging, and Production Environment Variables: How to Keep Them Separate
Most configuration accidents follow the same pattern. A developer runs a script with DATABASE_URL pointing at production. A staging deploy reads a key meant for local testing. A feature flag flips in the wrong environment because the names matched and nobody checked which file was loaded.
The fix is not more care. It is a structure that makes the wrong environment hard to reach. This guide shows how to keep local, staging, and production configuration separate without a heavy setup.
Why environments drift
Environments start out similar and slowly diverge. Someone adds a variable to staging and forgets production. Someone copies a .env file to a new machine and edits one value. After a few months, nobody is sure which values are real.
Three habits cause most of the drift:
- One file for everything. A single
.envthat gets edited for each environment. - Shared names, different meanings.
API_URLmeans localhost in one file and a load balancer in another. - Manual copying. Values get moved by hand, which means they get moved wrong.
Give every environment its own scope
Treat each environment as a separate set of values, not a variant of one file. Name the environments clearly, and keep them in separate records or files:
development local laptops, test accounts
staging pre-production, realistic data shape
production real users, real money
Each variable should exist in every environment where the app needs it. If a variable is missing in staging, the deploy should fail, not fall back silently to a default that points somewhere else.
Fail fast on missing or wrong values
Validate configuration when the app starts. Check that required variables exist and that they look right for the environment:
const required = ["DATABASE_URL", "STRIPE_SECRET_KEY", "APP_ENV"];
for (const name of required) {
if (!process.env[name]) {
throw new Error(`Missing ${name}`);
}
}
if (process.env.APP_ENV === "production" && process.env.DATABASE_URL.includes("localhost")) {
throw new Error("Production is pointing at localhost");
}
A startup check costs a few lines. It catches the most expensive mistakes before they reach a user.
Make the environment explicit
Do not rely on the machine to tell you where it is. Set an APP_ENV value in every environment, and log it at startup. When someone reads the logs, they should see which environment the process believes it is in.
For scripts that touch data, require the environment as an argument:
npm run migrate -- --env staging
Make production require an extra confirmation or a dedicated command. The goal is that running the wrong thing takes a deliberate choice.
Use different credentials, not just different values
Staging should never use production keys. That includes payment providers, email services, and third-party APIs. Use test-mode keys for non-production environments, and keep their scope narrow. If a staging key leaks, it should not be able to touch real customer data.
Keep local files local
Your local .env file is for development only. Keep it out of git, and do not copy it to servers. Production and staging should receive their values from the platform or from a store that controls access by environment.
A simple layout:
.env.example committed, placeholder values
.env local only, ignored by git
Who can change what
Access should follow environment. Most engineers need read access to development and staging. A smaller group should be able to change production. Review that list when people join or leave, and when the team changes shape.
Role-based access makes this practical. It is much easier to answer "who can read production?" when the answer is one short list, not a set of files shared over time.
Checklist
- Each environment has its own complete set of values
APP_ENVis set in every environment and logged at startup- The app validates required variables before it serves traffic
- Staging and local use test-mode credentials only
- Production access is limited to a small, reviewed group
- No environment falls back silently to another environment's values
Separation is what makes configuration calm. When each environment is clearly labeled and checked, a mistake becomes a failed startup instead of an incident.
