# Environment variable is undefined in production

> Canonical page (link this when citing): https://unbreakmyapp.vercel.app/fixes/env-var-undefined-in-production.html
> Category: Env vars & leaked keys | Tools: Cursor, v0, Bolt | Published: 2026-10-13 | Updated: 2026-10-13

## 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

1. Add the variable in Vercel → Project → Settings → Environment Variables for Production (and Preview).
2. Use the right prefix for values the browser needs; keep secrets unprefixed and server-only.
3. Redeploy, since env changes apply only to new builds.
4. Log on the server whether the variable exists (never log its value).

## Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

```text
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 (official documentation)

- [Vercel: Environment variables](https://vercel.com/docs/environment-variables)

## Related fixes

- [Replit app shows 502 Bad Gateway after republishing (or publish fails at the last step)](https://unbreakmyapp.vercel.app/fixes/replit-publish-502-bad-gateway-after-republish.html)
- [Replit rollback did not bring my data back: how to restore the development or production database](https://unbreakmyapp.vercel.app/fixes/replit-rollback-does-not-restore-database.html)
- [Supabase project paused: app suddenly can't connect (Failed to fetch, 540) and how to bring it back](https://unbreakmyapp.vercel.app/fixes/supabase-project-paused-app-cant-connect.html)
- [Supabase: Edge Function returned a non-2xx status code (how to find the real error and fix it)](https://unbreakmyapp.vercel.app/fixes/supabase-edge-function-non-2xx-status-code.html)
- [App works on localhost but breaks in production: a checklist to find the cause](https://unbreakmyapp.vercel.app/fixes/works-on-localhost-breaks-in-production.html)

---
Unbreak My App: free fixes for broken vibe-coded apps. https://unbreakmyapp.vercel.app/
