Home / Fix guides
Cursorv0BoltLovableViteNext.js

Environment variables are undefined in production (Vite and Next.js apps from Cursor, v0 and Bolt)

Updated 2026-11-11

Error: supabaseUrl is required. / import.meta.env.MY_KEY is undefined / process.env.MY_KEY is undefined in the browser

What you see

It works locally with your .env file. In production the page is blank or the console shows something like:

Error: supabaseUrl is required.

or your code reads undefined where a key or API URL should be.

Why it happens

There are three usual causes.

1. The .env file never reached the host. It is (correctly) in .gitignore, so Vercel, Netlify or Cloudflare never see it. You have to enter the same variables in the host’s dashboard.

2. The variable has no public prefix. Browser code only gets variables that are explicitly marked public:

3. You changed the value but didn’t rebuild. Both Vite and Next.js replace these variables with their literal values at build time. Next.js’s docs warn that after the build, the app “will no longer respond to changes to these environment variables.” Editing a value in the dashboard does nothing until you redeploy.

Fix it

  1. List every variable your browser code reads: search the code for import.meta.env or process.env.NEXT_PUBLIC_.
  2. Add each one, with its exact name, in your host’s environment settings for the Production environment (and Preview if you use it).
  3. Check the prefix matches your framework (VITE_ or NEXT_PUBLIC_).
  4. Trigger a new deployment so the build picks up the values.

Next.js also supports dynamic lookups like process.env[name], but they are not inlined in the browser. Reference each variable by its full literal name.

The security part (read this)

Anything with a VITE_ or NEXT_PUBLIC_ prefix is pasted into your public JavaScript bundle, where anyone can read it. Vite’s docs say plainly that VITE_* variables “should not contain sensitive information such as API keys.”

If a secret was ever bundled or committed, rotate it at the provider. Run a secret scanner over the repo history before you make it public.

Check it worked

After redeploying, open DevTools → Network and confirm requests go to the right API URL. Then search the built JS (Sources tab) for the first few characters of your secret keys: they must not be there.

Sources

Tools mentioned

Hosting

Vercel

Git-connected hosting with a global CDN, serverless functions and preview deployments for every push. The default target for Next.js and v0 projects, and it also deploys Vite apps exported from Bolt or Lovable.

Free: Hobby plan: $0/month, 1 developer seat
Hosting

Netlify

Static and serverless hosting with deploy previews, functions, forms and a CDN. Popular for Vite/React SPAs exported from AI builders.

Free: Free plan: $0 forever with a 300-credit monthly limit
Security

GitHub Secret Scanning Push Protection

Blocks git pushes that contain recognized secrets (API keys, tokens, private keys) before they land in your repository.

Free: Push protection for users is on by default for every GitHub.com account
Security

Gitleaks

An open-source command-line tool that scans git repositories, files and history for hardcoded secrets. Can run locally, as a pre-commit hook or in CI.

Free: Open source and free to run