Fix guides · Lovable, Vite, Supabase ·
Fix Vite/Lovable environment variable exposure in browser builds
Variable is not defined in browser / Vite build didn't include it
Vite exposes variables with VITE_ prefix to the browser build by default; projects can change envPrefix in vite.config. Non-VITE_ variables stay undefined even when set in .env or project settings.
Common causes – and how to tell which one you have
1. Missing VITE_ prefix on env var
How to tell: A variable without VITE_ prefix stays undefined in browser code. The crash appears only when code uses the value (for example, createClient called with undefined URL or token).
Fix: Add the VITE_ prefix to the variable name in your .env file or build environment. Vite then embeds the value into the browser bundle.
How to fix it
- Check browser console for undefined variable behavior during page load.
- Confirm which environment variables have VITE_ prefix in your project configuration.
- In Vite: use import.meta.env.VITE_VARNAME in your code and set the variable with VITE_ prefix in your build environment (e.g., .env file, then rebuild or restart dev server).
- Publish and reload the page after a rebuild.
- Never add secret keys with the VITE_ prefix — only public values such as the Supabase anon key belong in VITE_ variables. Unprefixed variables are not exposed to the browser bundle. The Supabase anon key is public by design; data safety comes from Row Level Security policies, not from hiding the key.
Using Lovable, Bolt or v0?
In Vite: variables with VITE_ prefix are exposed to the browser. The Supabase anon key is public by design — data safety comes from Row Level Security policies, not from hiding the key.
Using Cursor, Claude Code or Windsurf?
Ask the agent to first capture the exact console error, then check whether the missing variable has the VITE_ prefix. Guide the user to add it with that prefix, then publish and verify.
The “fix” to avoid
Adding the variable without VITE_ prefix keeps it undefined. Adding secret keys WITHOUT VITE_ prefix keeps them out of the browser bundle and safe. The real risk is adding secrets WITH VITE_ prefix, which embeds them in the browser bundle visible to all visitors.
Copy-paste fix prompt for your AI coding tool
Paste this into Cursor, Claude Code, Lovable or Bolt:
My Vite/Lovable app has undefined variables in the browser. First check: which environment variables are defined? Use import.meta.env.VITE_VARNAME to check. Then: add the variable with VITE_ prefix in your build environment. Secret keys must never get the VITE_ prefix. Rebuild or restart dev server, publish, and verify. Never add secrets with VITE_ prefix.