The notes on Dodi’s bench: .env, .env.local, .env.[mode], .env.[mode].local, and a variable from the shell. Change the mode and the files: whose value wins?…
Why it matters
"The value differs from what I wrote in .env" almost always means another file read later, a different mode (vite build = production), or a shell variable from CI. Knowing the order makes a strange value traceable, and knowing *.local keeps secrets out of git.
The value is set in .env, but a different one is used Look for a file read later: .env.local, .env.[mode], .env.[mode].local, then shell variables (CI often sets them). The last one wins.
A secret in .env.local, but still worried it gets committed Make sure .gitignore holds *.local, and never git add -f that file. If it was ever committed, the secret is in git history: replace the key. Remember too: a VITE_ value in .env.local is still sent to the browser.
.env.development is read locally, gone in the production buildvite build uses mode production, so what is read is .env.production. Put production values there (or in .env), or build with the --mode you mean.
Try it in this playground
See .env.development.local win The rightmost note overrides the other three.
Run vite build with no --mode.env.development is not read.
Win with the shell One shell variable overrides every file.
See # cut a valueVITE_WARNA=#ff8800 unquoted becomes empty.