Cloudflare DevelopersCD
Cloudflare Developers5mo ago
19 replies
Baran

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
...
  "scripts": {
    "build": "tsc -b && NODE_OPTIONS=--max-old-space-size=4096 vite build",
    ...
  },
  "devDependencies": {
    "@cloudflare/vite-plugin": "^1.13.1", // latest
    "wrangler": "^4.37.0" // latest
    ...
  }
...


./wrangler.jsonc
// nothing related to env variables, and I want to keep it like that; since my source of truth is `.env` files


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 days
Was this page helpful?