Mendlark / Guides
Environment variables undefined in production on Vercel: how to fix it
Everything works on your machine. You deploy to Vercel and suddenly process.env.SOMETHING is undefined, Supabase says supabaseUrl is required, or Stripe complains about a missing key. Your .env file never left your laptop, and Vercel has its own rules about which variables reach which code. Work through these checks in order.
1. Your .env file is not deployed
.env and .env.local are (correctly) in .gitignore, so GitHub and Vercel never see them. Every variable your app uses must be added in Vercel: Project → Settings → Environment Variables. Search your code for process.env and import.meta.env to get the full list, and copy the names exactly. Capitals and underscores must match.
2. Tick the right environment
Each variable in Vercel is assigned to Production, Preview and/or Development. A variable added only to Preview is undefined on your live domain. Edit each one and make sure Production is ticked.
3. Browser code needs a public prefix
Frameworks hide variables from the browser unless the name says they're public:
- Next.js:
NEXT_PUBLIC_, e.g.NEXT_PUBLIC_SUPABASE_URL - Vite (Lovable, Bolt):
VITE_, read withimport.meta.env.VITE_SUPABASE_URL, notprocess.env - Create React App:
REACT_APP_
// Next.js client component
const url = process.env.NEXT_PUBLIC_SUPABASE_URL // works
const url = process.env.SUPABASE_URL // undefined in the browser
Only public values belong here (Supabase anon key, Stripe pk_live_…). Never give a Stripe secret key or Supabase service_role key a public prefix: anyone can read it.
4. Redeploy after every change
Public variables are baked in when the site builds. Adding or editing one in Vercel does nothing to the deployment that's already live. Go to Deployments, open the latest, and choose Redeploy (untick "use existing build cache" if offered).
5. Secret keys only work on the server
A variable without a public prefix (STRIPE_SECRET_KEY, SUPABASE_SERVICE_ROLE_KEY) is available in API routes, server actions and server components, never in code marked 'use client'. If a secret is undefined, check the file it's read in actually runs on the server. Move the call into an API route and fetch that from the browser.
6. Watch for dynamic access and typos
- Next.js replaces
process.env.NEXT_PUBLIC_Xas literal text at build time.process.env[name]or destructuringconst { NEXT_PUBLIC_X } = process.envcan come backundefinedin the browser. Write each one out in full. - Extra spaces or quotes pasted into the Vercel value field break keys. Paste the bare value.
- Run
vercel env pulllocally to see exactly what Vercel has.
Quick checklist
- Every variable from your code added in Vercel, names matching exactly
- Production ticked for each one
- Browser variables use
NEXT_PUBLIC_/VITE_; secrets don't - Redeployed after changes
- Secrets only read in server code; no dynamic
process.envlookups
Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, start with a free scan, or read our guide to a blank page after deploying a Vite app.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.