import.meta.env variable is undefined after deploying to Vercel or Netlify
Updated
import.meta.env.VITE_SUPABASE_URL is undefinedVite writes variables into the code at build time, and only those whose names start with VITE_. If the variable is missing in production, it was not set in the host’s settings for that environment, it has no VITE_ prefix, or the site was not rebuilt after you added it. Set it, then redeploy. If the value is a secret, don’t add VITE_: that publishes it in the browser bundle.
Go through the three causes
- Prefix. Only
VITE_variables reach browser code.SUPABASE_URLisundefinedthere,VITE_SUPABASE_URLworks. - Environment. Vercel and Netlify keep separate values for production, preview and development. A variable added only to Preview is empty on the production site.
- Rebuild. Values are baked into the JavaScript during the build. Changing them in the dashboard does nothing until the next deploy.
In v0 and other Next.js apps the same rule uses the NEXT_PUBLIC_ prefix.
When the value is a secret
The Supabase URL and anon key are meant to be public, so VITE_ is fine for them. OpenAI, Stripe secret and Supabase service role keys are not. If an agent suggests renaming OPENAI_API_KEY to VITE_OPENAI_API_KEY to make the error go away, it is putting the key into every visitor’s browser. Move that call into a server function instead.
Check that it worked
After redeploying, open the site, then DevTools → Sources and search the bundle for the variable’s value. Public values should be there. Secrets should not appear anywhere.