.env.local.production StrategyIf you’ve ever deployed a web application, you know the anxiety of environment variables. You have your .env file for local development, your CI/CD pipelines for deployment, and hopefully, you are dutifully ignoring your .env files in your .gitignore.
But what happens when you need to test a production build locally? Or when you want to keep your local development secrets separate from your local production secrets?
Enter the often-overlooked hero of the environment hierarchy: .env.local.production.
Why would you need a local file for production? Typically, you don't. But here are three specific scenarios: .env.local.production
next build in a production-like environment, but you need to inject temporary secrets (e.g., CI=true).Crucially, .env.production.local is ignored by Git by default in frameworks like Next.js. You have to explicitly add !.env.production.local to .gitignore if you want to commit it (which you almost never do).
If you are using dotenv directly:
require('dotenv').config( path: '.env.production.local' );
You must manually handle the loading order. Stop Committing Production Secrets: Why You Need a
STRIPE_SECRET_KEY="sk_live_12345..."
If you use Docker, mount a separate .env file:
docker run --env-file ./docker/prod-override.env myapp:latest
.env.production.local out of the box.next start runs (production build).next build unless NODE_ENV=production..env.local.production?The file name .env.local.production (or .env.production.local) is an environment-specific, machine-local override file. Staging debugging: You are running a production build
Let's break down the anatomy:
.env : The file type..production : The environment context. (Loaded when NODE_ENV=production)..local : The override flag. (Prevents committing to Git).If you have .env and .env.production, why introduce a third file? The answer lies in sensitive, environment-specific configuration.
Here are three scenarios where .env.local.production (or its equivalent) is indispensable.
Authorization is required to save your favorites and personal settings.