Fixes › Env vars & leaked keys
Environment variable is undefined in production
Published
The error
process.env.X is undefined / import.meta.env.VITE_X is undefined after deploy
Why it happens
Your .env file is not uploaded when you deploy. Variables must be added in the hosting dashboard (Vercel, Netlify), and client-side values need the framework prefix (NEXT_PUBLIC_ for Next.js, VITE_ for Vite). Values are embedded at build time, so you must redeploy after adding them.
How to fix it
- Add the variable in Vercel → Project → Settings → Environment Variables for Production (and Preview).
- Use the right prefix for values the browser needs; keep secrets unprefixed and server-only.
- Redeploy, since env changes apply only to new builds.
- Log on the server whether the variable exists (never log its value).
Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)
Environment variable <NAME> works locally but is undefined in production on Vercel. Check whether it's read in client or server code, rename it with NEXT_PUBLIC_/VITE_ only if it is safe to be public, otherwise move the usage to server code. List the exact variables I must add in the Vercel dashboard and remind me to redeploy.
FAQ
Why does it work locally?
Your local dev server reads .env; the deployed build doesn't have that file.
Do I need to redeploy?
Yes. Env vars are injected at build/deploy time.
Sources
Based on the official documentation:
Related fixes
- Replit app shows 502 Bad Gateway after republishing (or publish fails at the last step)
- Replit rollback did not bring my data back: how to restore the development or production database
- Supabase project paused: app suddenly can't connect (Failed to fetch, 540) and how to bring it back
- Supabase: Edge Function returned a non-2xx status code (how to find the real error and fix it)
- App works on localhost but breaks in production: a checklist to find the cause
Want the full checklist for this area? Get a Fix Kit ($29)
Featured slot available: reach thousands of vibe coders. Sponsor this week →
Get one new fix a week
Free newsletter: the newest breakages in Lovable, Bolt, Cursor, Replit and v0 apps, with paste-in fixes.
We only use your email to send the newsletter. Unsubscribe any time. See privacy.