Vite env variable undefined in production (blank white page)
Works locally, white screen after deploy, and import.meta.env is undefined? Vite bakes variables in at build time. Here are the six causes and how to confirm each.
4 min readvite, environment variables, deployment, error fix
Short answer: Vite does not read environment variables when your site runs. It copies their values into the code once, at build time. If a variable is missing from the build, is not prefixed with VITE_, or is read in a way Vite cannot replace, it is undefined in production, and the app often shows a blank white page. Fix the variable where the build happens, then rebuild.
This is a common trap for sites made with Lovable, Bolt and v0, which use Vite, and which work in the preview and break on a new host.
Why it breaks only in production
On your computer, Vite reads a .env file. On Netlify, Vercel, Cloudflare or another host, that file is not there, because it should not be uploaded. The host builds your site from scratch, and the build only sees variables you entered in the host's settings. If you did not enter them, the build bakes in undefined.
A missing value usually fails quietly until your code tries to use it, for example when a database client is created with no address. That is the blank page.
Confirm it in two minutes
Open the live site, press F12, and look at the Console. A line such as Cannot read properties of undefined or supabaseUrl is required points at a missing variable. Then check which variable.
Cause 1: the variable was not set at build time
The most common cause by far. Add it in your host's environment settings (Site settings, Environment variables, or Project settings), then trigger a new deploy. Changing a variable does not change a site that is already built.
Cause 2: the name has no VITE_ prefix
Vite exposes only names that start with VITE_. This is by design, so server-only values do not leak.
# not available in the browser
API_URL=https://api.example.com
# available as import.meta.env.VITE_API_URL
VITE_API_URL=https://api.example.com
Also, in Vite code you read them with import.meta.env.VITE_API_URL. The Node-style process.env.VITE_API_URL does not exist in the browser.
Cause 3: the variable is read in a way Vite cannot replace
Vite swaps the exact text import.meta.env.VITE_NAME for its value. These patterns are not recognised in a production build:
const key = "VITE_API_URL";
import.meta.env[key];
const { VITE_API_URL } = import.meta.env;
Write the full dotted name each time. If you must choose a variable by name, use a small object that lists each full reference.
Cause 4: wrong file or location
The .env file belongs in the project root, beside vite.config, not inside src. Vite also loads different files by mode: vite build reads .env.production, while a value that lives only in .env.development is missing from the production build.
Cause 5: you restarted nothing
After editing .env locally, restart the dev server. After editing host settings, redeploy.
Cause 6: you put a secret in a VITE_ variable
This one is not a bug, it is a hazard. Anything with the VITE_ prefix ends up in the public JavaScript. That is fine for a Supabase URL and its public key. It is not fine for a Stripe secret key, an OpenAI key or a service_role key. If you have done that, see what to do when an API key is exposed.
Check what the build contains
Build locally and search the output:
npm run build
grep -r "your-project-url" dist | head
If the value is not in the build, the problem is at build time. If it is there but the site still fails, the problem is elsewhere, so read the console error again.
Making a blank page less mysterious
A blank page tells you nothing. Add error reporting so you see the cause without hunting: your app is broken right now and nobody has told you covers the small setup. And make the app fail loudly at start-up, with a clear message, if a required variable is missing.
Ask your AI tool
"The production build shows a blank page and import.meta.env values are undefined. List every environment variable this app uses, confirm each starts with VITE_ if it is needed in the browser, replace dynamic access with full import.meta.env.VITE_NAME references, show an error screen if a required variable is missing, and write an .env.example file with the names only."
FAQ
Why does import.meta.env work locally but not on Vercel?
Locally it reads your .env file. On Vercel the build only sees variables entered in the project settings.
Do I need to redeploy after changing a variable?
Yes. Vite inlines the value during the build.
Can I change a Vite variable without rebuilding?
Not for the built-in kind. For values that must change at runtime, serve a small config file or endpoint that the app fetches on load.
Is VITE_ safe for API keys?
Only public ones. Everything with that prefix is visible to every visitor.
Launching soon?
Settings that differ between preview and live are a main cause of launch-day breakage. Send us a link and we will walk the real domain with fresh eyes.