Environment variables are undefined in production
Works locally but env vars are undefined once deployed? Add them to your host, use the VITE_ or NEXT_PUBLIC_ prefix for browser code, then redeploy.
Last edited October 30, 2026
On this page
What it means in plain English
Your app reads settings (API keys, URLs) from "environment variables". On your computer they come from a .env file. That file is deliberately not uploaded, so the live site has none of them unless you add them to your hosting provider.
Deploy Without Errors Pack
Environment variables on Vercel, Netlify and Replit, SPA refresh rewrites, npm peer dependency errors and domain setup.
Secure checkout by Stripe
One-time payment through Stripe. Priced in USD. Local taxes such as UK VAT may be added at checkout.
Likely causes
- The variables were never added in Vercel/Netlify project settings.
- Browser code reads a variable without the required prefix. Vite only exposes variables starting
VITE_; Next.js only exposesNEXT_PUBLIC_variables to the browser. - You added the variable but didn't redeploy. Browser variables are baked in at build time.
- The variable was added only for Preview, not Production (or the other way round).
- The AI assistant invented a variable name that doesn't match the one you set.
How to fix it, step by step
- Find the exact names your code uses: search the project for
import.meta.env.(Vite) orprocess.env.(Next.js). - Vercel: Project → Settings → Environment Variables → add each name and value, tick Production (and Preview if needed).
- Netlify: Site configuration → Environment variables → add each one.
- If the value is needed in the browser, the name must start with
VITE_(Vite/Lovable/Bolt) orNEXT_PUBLIC_(Next.js/v0). Never put secret keys behind these prefixes: anything prefixed ends up in public JavaScript. Never add that prefix to a secret such as an OpenAI key. See OpenAI API key in the browser. - Trigger a new deployment (Vercel: Deployments → ⋯ → Redeploy). Changes don't apply to old deployments.
- In Cursor, tell the AI: "Only use these env var names: …, never invent new ones."
How to check it worked
- Redeploy and reload with cache disabled; the feature using the variable should work.
- In Vercel, open the deployment's Build Logs and confirm no 'missing env' warnings.
Sources
Related fixes
Tool to consider: Vercel.
Deploy Without Errors Pack
Environment variables on Vercel, Netlify and Replit, SPA refresh rewrites, npm peer dependency errors and domain setup.
Secure checkout by Stripe
One-time payment through Stripe. Priced in USD. Local taxes such as UK VAT may be added at checkout.