Incorrect `.env` file with Vite deployment
Hi all,
My aim:
- Cloudflare preview links to run with
.env.development file
- Cloudflare production deployment to run with .env.production file
My current setup:
./package.json
./wrangler.jsonc
Cloudflare GUI:
- Build command for both: npm run build (I can't change this because of@cloudflare/vite-plugin limitation, source: https://developers.cloudflare.com/workers/wrangler/configuration/#custom-builds)
- Non-prod deploy command: npx wrangler versions upload
- Prod deploy command: npx wrangler deploy
Current result:
Both Preview and Production builds load .env.production.
---
How do I achieve this? I am stuck for the past few days9 Replies
You need to specify the environment for Vite - https://developers.cloudflare.com/workers/vite-plugin/reference/secrets/
so you'd need to do
CLOUDFLARE_ENV=staging vite build and CLOUDFLARE_ENV=production vite buildbut how do i achieve this when i have only 1 build command?
https://developers.cloudflare.com/workers/wrangler/configuration/#custom-builds

you'd need to do it as part of the deploy commands -
npm run build && npx wrangler deploy and npm run build:staging && npx wrangler versions upload
This will hopefully be nicer in the future
cc: @texani did this, and preview deployments are still loading production env variables despite logging "development".
Cloudflare GUI:
- Build command (Optional)
echo "building..."
- Deploy command
CLOUDFLARE_ENV=production npm run build && npx wrangler deploy
- Non-production branch deploy command (Optional):
CLOUDFLARE_ENV=development npm run build && npx wrangler versions upload
--
Logs:
2025-09-15T13:47:25.444Z [env.development]
2025-09-15T13:47:25.445Z No environment found in configuration with name "development".You don't have an environment development
but shouldn't wrangler pickup my env file?
the rest of the logs are even more confusing
2025-09-15T13:47:26.211Z Using vars defined in .env.developmentbut i am confident that vars in
.env.production were used for this preview link
i added
to get rid of the error, and its still using the variables in .env.production
SOLVED ✅
This shouldn't have taken 2 days, the docs for this are conflicting/outdated/confusing.
package.json
wrangler.jsonc
.env.development
.env.production
Cloudflare GUI (Workers > your worker > Settings):
Build
Edit your build configuration settings
Build configuration
Build command
echo "building..."
Deploy command
CLOUDFLARE_ENV=production npm run build && npx wrangler deploy
Non-production branch deploy command
CLOUDFLARE_ENV=development npm run build && npx wrangler versions upload
Result:
- Cloudflare preview links load .env.development
- Cloudflare production link loads .env.production@Baran how are you accessing your .env files? are you committing your .env.* files to git?
my
.env.* files don't contain any private environment variables/secrets, so i push them to git
i upload the private env vars/secrets via cloudflare gui or cli
--
I access via process.env. or import.meta.env. in code.