My Production Env Changes Did Nothing. A Forgotten .env.local Was Winning.
In short
Next.js reads .env.local before .env.production and stops at the first match, so a forgotten override file wins without any error. On my VPS that first kept a stale payment credential alive, then a committed copy pointed sign-in at a deleted Supabase project for about 19 hours. My rules now: **run ls -la .env* first, never commit .env.local, make the deploy remove or refuse override files, and read the build's - Environments: line** after every deploy.

On this page
- Why do my .env.production changes do nothing in Next.js?
- What is the Next.js environment variable load order?
- How did a deleted .env.local come back after a deploy?
- Why does Supabase sign-in fail with "Failed to fetch" and no status code?
- How do I check which env files a Next.js build actually used?
- What else can go wrong with .env files on a VPS?
- How do I stop .env.local from overriding production for good?
- What would I do differently next time?
Short answer: Next.js looks for .env.local before .env.production and stops at the first file that defines a variable, so any value set in both is served from .env.local. If one is sitting in your app directory, you can edit .env.production, restart, and watch nothing change, with no error anywhere. On a Next.js app I run on a self-managed VPS this happened twice: first as a stale payment credential, then about three weeks later as a full outage, when a committed .env.local pointed sign-in at a Supabase project that no longer existed. The fix is boring: run ls -la .env* before debugging anything, never commit .env.local, make the deploy remove or refuse override files, and read which env files the build actually loaded.
Why do my .env.production changes do nothing in Next.js?
The first time, it looked like a caching problem. I changed the payment provider's client id in .env.production, restarted the app under pm2, tested again, and got the old credentials back. No error at start, nothing in the logs. The payment flow kept using a value I had already replaced.
The deploy script made the illusion stronger. It copies .env.production into the build directory and chmods it to 600, carefully, every time. Everything about the setup said that file was the source of truth.
It wasn't. This is the command I should have run first:
ls -la .env*There was a .env.local next to .env.production. It held 13 variables, and all 13 were duplicates of keys in .env.production. The only values that differed were the old payment pair. So every one of those keys was being served from .env.local, and my edits were going into a file Next.js never reached for them.
Because the other 11 values matched, nothing else looked wrong. That's what made it hard to spot. A shadow file that mostly agrees with the real one behaves exactly like the real one, right up to the day you change something.
I moved it out of the app directory rather than deleting it. That was safe, since every key in it also lived in .env.production.
What is the Next.js environment variable load order?
The Next.js environment variables docs ↗ spell it out. Variables are looked up in these places, in order, and the lookup stops once the variable is found:
1. process.env
2. .env.$(NODE_ENV).local (.env.production.local in production)
3. .env.local (not checked when NODE_ENV is test)
4. .env.$(NODE_ENV) (.env.production in production)
5. .envTwo things in that list matter on a VPS.
First, .env.local sits above .env.production. That's by design: the docs describe .env.local as intended to override the default set. The same page says .env*.local files are intended to be ignored through .gitignore, and that the default create-next-app template adds .env files to .gitignore.
Second, process.env beats every file. If your process manager or shell exports a variable, no file edit changes it either. When an env change "does nothing", there are three suspects above .env.production, not one.
There's no warning when a higher file wins. Next.js loads what it finds and gets on with it, which is reasonable for a framework and a trap on a production box where you assume one file. The only trace is a single line in the build output, which I'll come back to.
How did a deleted .env.local come back after a deploy?
Through git. A commit made during handover work put .env.local into the repository. It was on purpose: it came with an exception in .gitignore.
# re-includes a file that an earlier pattern ignores
!.env.localThe ! prefix negates a pattern, so a file excluded by an earlier pattern becomes included again (gitignore docs ↗). From that commit on, .env.local was a tracked file.
The deploy runs git checkout --detach --force, which writes every tracked file into the build tree. So about three weeks after I had cleared the file off the box, a deploy put the committed .env.local right next to the .env.production the script had copied in and checksummed, and it outranked it.
Every guard in the deploy passed. Here's why each one missed it:
git clean -fdxwas in the script. It removes files that are not under version control (git-clean docs ↗). This one was tracked, so it stayed.- The deploy verified
.env.productionwithsha256sum. That check passed, correctly. The file was copied perfectly and then simply outranked. - The smoke test asserted that the app answered. It didn't assert which database it answered from. The deploy that shipped the outage passed every check and exited 0.
No error appeared at deploy time. The first symptom was people failing to sign in.
Why does Supabase sign-in fail with "Failed to fetch" and no status code?
Because the request never reached a server. The committed .env.local held the URL of a Supabase project that no longer existed. Its hostname was NXDOMAIN, so the browser couldn't open a connection at all.
That changes what the failure looks like. In the network panel, sign-in and sign-up requests showed (failed) with 0 bytes, and the error was "Failed to fetch". Never a 4xx. MDN's fetch reference ↗ explains the split: a fetch() promise rejects on a network error, but does not reject when the server answers with an error status like 404 or 504. If you see a status code, a server answered. If you see "Failed to fetch" and 0 bytes, check DNS and the URL before you touch auth.
# does the Supabase host in the build even resolve?
dig your-project-ref.supabase.co | grep status
# "status: NXDOMAIN" means there is nothing to connect toSign-in wasn't the only thing that file broke. The email provider's API key in it was blank, so no email left the box, OTP codes included. And NEXT_PUBLIC_SITE_URL was http://localhost:3000.
It ran like that for about 19 hours.
How do I check which env files a Next.js build actually used?
next build tells you. Right after the version banner, it prints a line that starts with - Environments: and lists the env files it loaded, separated by commas. On a production box you want exactly one:
- Environments: .env.productionIf that line lists .env.local, .env, or anything else, the build is compromised, whatever the deploy's own checks say. I read it after every deploy now.
The second thing to know: variables prefixed with NEXT_PUBLIC_ are inlined into the JavaScript bundle during next build, as the same docs page ↗ says. Fixing .env.production and restarting changes nothing for those values. You need a rebuild.
To see what a live build actually points at, grep the build output instead of trusting the env files:
# which Supabase project is baked into the client bundle?
grep -rhoE "https://[a-z0-9]+\.supabase\.co" .next/static/chunks/*.js | sort -u
# the server side is built separately, so check it too
grep -rhoE "https://[a-z0-9]+\.supabase\.co" .next/server | sort -uIf either prints a host you don't recognise, you've found your shadow file.
What else can go wrong with .env files on a VPS?
Two more problems turned up on the day of the first cleanup. Both are fixed.
.env.production was mode 644, which is world-readable. On a dedicated box that's sloppy. This box also runs WordPress, as a different user, so anything running as that user could read the app's production secrets. It isn't world-readable any more.
The payment mode line carried an inline comment. It looked like this, with the variable name changed:
PAYMENT_MODE=live # change to "live" for productionWhether that comment gets stripped depends on the parser. The dotenv README ↗ says comments can go on their own line or inline, and that treating everything after a # as a comment is a breaking change from v15.0.0 on. So the difference between live and sandbox came down to whether a comment survived parsing. A comment should never get a vote on that. My rule now: comments go on their own line, never after a value.
How do I stop .env.local from overriding production for good?
The fix went in at two independent layers, so neither one can cause this alone:
.env.localis untracked again.- Both copies of the deploy script, the one on the box and the one in the repo, now delete override files from the build tree after checkout.
# after: git checkout --detach --force <ref>
rm -f .env .env.local .env.production.local .env.development*
# the .env.production copy, chmod 600 and checksum stay as they wereEither layer alone would have prevented the outage. With both, a future commit and a future script edit would each have to go wrong before production reads the wrong file again.
Comparing those two copies turned up something unrelated. The box copy was missing a block the repo copy has, the one that swaps in public/, so files added to public/ never reach production and 404 forever. I reported it and deliberately didn't change it mid-incident. Two copies of a deploy script are two different scripts sooner or later.
On new projects I'd go one step further and make the build refuse instead of tidying up. Deleting the file quietly fixes the symptom. Failing loudly tells you someone committed it. Illustrative, not the production script:
#!/usr/bin/env bash
set -euo pipefail
# 1. refuse to build with any override file in the tree
for f in .env .env.local .env.production.local .env.development*; do
if [ -e "$f" ]; then
echo "refusing to build: $f is in the build tree" >&2
exit 1
fi
done
# 2. build, keep the log, and check what Next.js actually loaded
npx next build 2>&1 | tee build.log
if ! grep -qE '^\s*- Environments: \.env\.production\s*$' build.log; then
echo "build loaded more than .env.production:" >&2
grep -E '^\s*- Environments:' build.log >&2 || true
exit 1
fiThen make the smoke test check which database the app talks to, not only that it answers: compare the Supabase host baked into .next with the one in .env.production, and fail the deploy if they differ.
And this is the checklist I run before debugging any "the env var didn't take" problem on a server:
[ ] ls -la .env* only .env.production in the app dir
[ ] git ls-files '.env*' nothing tracked but an example file
[ ] stat -c '%a' .env.production 600, not 644
[ ] grep -nE '^[^#].*\s#' .env.production no inline comments
[ ] build log "- Environments: .env.production"
[ ] NEXT_PUBLIC_* changed? rebuild, then grep .next for itWhat would I do differently next time?
I'd treat the first incident as a deploy bug, not a server bug. In August I fixed it on the box and moved on. The file came back about three weeks later through a path a server fix couldn't touch, because the real gap was that the deploy trusted whatever git handed it. If a production problem can be caused by a file existing, the deploy should check that the file doesn't exist, every time, and fail loudly when it does.
The rule I follow now is short. When an env change "did nothing", the first command is ls -la .env*, before logs, before restarts, before blaming a cache. The second is reading the - Environments: line from the last build. Those two checks take seconds, and between them they show every file that can outrank the one you're editing.
The smoke test lesson is the one that stings. Mine proved the app was up. It was up, and nobody could sign in. A check that only asks "does it answer?" will pass on a site pointed at a database that doesn't exist.
If your deploys pass every check and still ship the wrong config, that's the kind of gap I look for in SaaS security and monitoring ↗ work. Or get in touch ↗.
FAQ
Does .env.local override .env.production in Next.js?
Yes. Next.js looks up variables in process.env, then .env.production.local, then .env.local, then .env.production, then .env, and stops at the first place the variable is found. Any key defined in both .env.local and .env.production is served from .env.local. The only exception is when NODE_ENV is test, where .env.local is not checked.
Why didn't my Next.js environment variable change after a restart?
Either a higher-priority source still defines it, such as process.env, .env.production.local or .env.local, or the variable is prefixed with NEXT_PUBLIC_ and was inlined into the bundle at build time. Run ls -la .env* in the app directory first, then check the build log. NEXT_PUBLIC_ values only change after a rebuild, not a restart.
Should I commit .env.local to git?
No. The Next.js docs say .env*.local files are intended to be ignored through .gitignore. A committed .env.local gets written into every checkout, including the production build tree, where it outranks .env.production, and git clean will not remove it because it is tracked. If people need a starting point, commit an example file with no real values.
How can I tell which env files a Next.js build actually loaded?
Read the line in the next build output that starts with "- Environments:". It lists every env file the build loaded, and on a production server it should list only .env.production. To confirm what a NEXT_PUBLIC_ value resolved to, grep .next/static/chunks and .next/server for it, since client and server code are built separately.
Why does Supabase sign-in show "Failed to fetch" instead of an error code?
Because the browser never reached a server, so there is no status code to show. A fetch() promise rejects on network errors such as a hostname that does not resolve, while HTTP error statuses like 404 or 504 come back as normal responses. Check that the Supabase URL in your build resolves with dig before you debug auth. In my case the deleted project's hostname returned NXDOMAIN.
Working on something like this?
I build web apps, AI features, and mobile products for clients. If this article matches a problem you have, tell me about it.
Start a conversationMalik Hamza Shabbir · Full-Stack & AI Engineer
I build full-stack and AI products solo: a reputation SaaS in production, RAG pipelines, and React Native apps. I write from what I ship, not from documentation summaries.
Run it on yours
Every number above came from code you can read.
The harness is open and the per-probe log ships with it. If you build one of these systems, or you are choosing between them with real money, the same method is available to you.
Send me an endpoint and I will write an adapter, run it on the same workload under the same rules, and publish the result. It goes in the table whether it wins or not.
Sponsored inclusion is disclosed in the article and the result publishes either way. Paying moves you up the queue, never up the table.
Related articles
Supabase Sign-In Was Down and the Database Looked Fine. The 401 Was Lying.
During a production sign-in outage, GET /rest/v1/ kept answering 401 in about 0.15s while Supabase auth, PostgREST and storage were dead. These are the probes that tell the truth, the Management API calls that fixed it, and a ten-second runbook.
Our New Cold Email Domains Were Flagged Before They Sent a Single Email
Ten new cold email domains were flagged on Validity's Heatwave blocklist while in Smartlead's warmup pool, before sending one real email. How warmup led to Spamhaus and SURBL listings, what we ruled out, and the hourly guard we run now.
Our LLM Fallback Worked Perfectly. That Was the Problem.
During a model outage, a fallback on a review-reply product I lead posted generic English replies to Swedish businesses' Google profiles for days, and every dashboard stayed green. This is the publish gate and the per-path fail-closed rule I use now.