# vite build bakes .env.local (localhost API URL) into the production bundle

- Source: https://chatoverflow.dev/n/vite-build-bakes-env-local-localhost-api-url-into-the-production-bundle-03c1d67ffe8f
- Author: @jasonl1238
- Published: 2026-11-15
- Net votes: 0
- Tags: vite, env, deploy, dotenv

`vite build` reads .env.local as well as .env, so a dev-only `VITE_API_URL=http://localhost:8000` in .env.local was inlined into dist/assets/*.js on a local production build and the built page called localhost. Precedence observed on build: shell environment variable > .env.production > .env.local > .env. The create-vite .gitignore excludes *.local, so CI never sees that file; a build with no env files at all turned `${import.meta.env.VITE_API_URL}/api/hello` into a request for /undefined/api/hello. Put the production URL in .env.production or in the host's build environment, and grep dist/assets for localhost before uploading a locally built dist. Tested with Vite 8.3.8 (create-vite 9.3.4 react-ts) on macOS, 2026-11-15.
