November 22, 2026 · 9 min read · Thomas Osayi
Environment variables undefined on Vercel? Every cause and fix
Works locally, undefined on Vercel? Fix missing NEXT_PUBLIC_ and VITE_ prefixes, wrong Production or Preview scope, the missing redeploy and build-time vs runtime mixups.
You deploy to Vercel, open the site, and the console says "Error: supabaseUrl is required." or "FirebaseError: Firebase: Error (auth/invalid-api-key)." Or a fetch goes to "undefined/api/orders". Everything worked a minute ago on localhost. The variable is right there in your .env file.
This is the single most common reason an AI-built app that works locally breaks on its first real deploy. The good news is that there are only five or six causes, and you can check them all in about fifteen minutes. I will go through them in the order I find them, with the exact screens, prefixes and files involved.
Why are my environment variables undefined on Vercel?
Because Vercel builds your app from your Git repository and its own dashboard settings, not from the .env files on your laptop.
Ranked by how often I see them:
- The variable only exists in .env.local, which is listed in .gitignore, so Vercel never received it.
- It is set in Vercel but only for Production, and you are looking at a Preview deployment, or the other way round.
- It was added or changed after the last build, and nobody redeployed.
- Browser code reads a variable without the framework prefix: NEXT_PUBLIC_ in Next.js, VITE_ in Vite.
- The code reads it dynamically, for example process.env[key] or a destructured copy of process.env, so the build never replaces it.
- The name has a typo, a different case, or the pasted value includes quotes or a trailing space.
Each one produces the same symptom, an undefined value, so the trick is to check them in order instead of guessing.
Did I actually add the variable in Vercel?
Check Project Settings, then Environment Variables, because if a variable is not listed there, Vercel does not have it.
The default Next.js template adds .env files to .gitignore, and the Next.js docs warn that you almost never want to commit them. Lovable, Bolt and Cursor projects usually follow the same pattern. That is correct for security, but it means the values on your machine never reach Vercel.
To add them, open your project on vercel.com, go to Settings, then Environment Variables. You can add them one by one, or paste the whole contents of a .env file into the Key field, which the Vercel docs say creates one variable per line. There is also an Import .env button. Before saving, review each variable's type and which environments it applies to.
Two details matter here. First, if you use a src folder in Next.js, the docs note that .env files are loaded only from the project root, not from inside src. Second, if your repository is a monorepo and the Vercel project's Root Directory points at a subfolder, the variables belong to that Vercel project, so add them there.
Is the variable set for the right environment?
Each variable applies only to the environments you tick, so a value set for Production alone is undefined on every Preview deployment.
Vercel has three standard environments, per its environment variables docs. Production applies to deployments from your production branch, usually main, or from vercel --prod. Preview applies to deployments from any other branch. Development is used when you run vercel dev locally and is what vercel env pull downloads into .env.local.
Preview variables can also be scoped to a single Git branch, and a branch-specific value overrides the general Preview value with the same name. That is handy for a staging database, and also a common source of confusion when one branch reads different values from every other.
The Vercel docs list a related trap in their troubleshooting table: if your code branches on VERCEL_ENV and you use a custom environment, VERCEL_ENV reports preview there. Read VERCEL_TARGET_ENV if you need the custom environment's name.
When I audit a project, I open the variable list and look at the environment badges on each row. A Supabase URL ticked for Production but not Preview explains why "the preview link is broken but the live site works" in seconds.
Did I redeploy after changing it?
Probably not, and you need to, because Vercel only applies variable changes to new deployments.
The Vercel docs are explicit: changes to environment variables are not applied to previous deployments, they only apply to new ones. The running deployment keeps the values it was built with. Their troubleshooting table lists "a change in the dashboard has no effect" as a symptom and "redeploy" as the fix.
To redeploy without a new commit, open the Deployments tab, find the latest deployment for the environment you changed, open its menu and choose Redeploy. For Production you can also push any commit to main. Then hard refresh the browser so you are not served cached JavaScript.
This matters even more for public variables. Next.js replaces every reference to a NEXT_PUBLIC_ variable with its literal value during next build. The Next.js docs say that after being built, your app will no longer respond to changes to these variables, and that they are frozen with the value evaluated at build time. So the build has to happen after the value exists.
Why is my NEXT_PUBLIC_ variable undefined in the browser?
Either it lacks the prefix, it is read through a dynamic lookup, or it did not exist when the build ran.
In Next.js, non-prefixed variables are only available on the server. That is deliberate. A STRIPE_SECRET_KEY read inside a client component will always be undefined in the browser, and it should be. If a value truly needs to be public, such as a Supabase URL and anon key, a Firebase web config or an analytics ID, rename it with NEXT_PUBLIC_ in both Vercel and your code.
The second cause is subtle. The Next.js docs show that dynamic lookups are not inlined. These patterns fail in client code:
- const name = "NEXT_PUBLIC_API_URL" followed by process.env[name]
- const env = process.env followed by env.NEXT_PUBLIC_API_URL
- A config helper that loops over a list of keys and reads each from process.env
Write process.env.NEXT_PUBLIC_API_URL literally, every time, wherever the browser needs it. A small config file that exports each value by its full literal name is fine, because the build can see and replace those references.
Why are my Vite or Lovable variables undefined?
Vite only exposes variables that start with VITE_, and you read them through import.meta.env, not process.env.
Lovable and Bolt projects are usually Vite apps under the hood. The Vite docs say that only variables prefixed with VITE_ are exposed to client code, and others, like a DB_PASSWORD in their example, stay undefined in the browser. So the variable must be named VITE_SUPABASE_URL in Vercel and read as import.meta.env.VITE_SUPABASE_URL in code.
Common mistakes when a Lovable app moves to Vercel:
- The variable is added in Vercel as SUPABASE_URL without the prefix.
- The code was edited to process.env.VITE_SUPABASE_URL, which Vite does not populate in the browser.
- The project was set up with a custom envPrefix in vite.config, so the expected prefix is different from VITE_.
Like Next.js, Vite replaces these values statically at build time, so the same redeploy rule applies. The Vite docs also warn that VITE_ variables should not contain sensitive information, because they ship in the bundle. My guide "How to move a Lovable app to Vercel and your own domain" covers the rest of that migration.
What is the difference between build-time and runtime variables?
Public variables are baked into the JavaScript during the build, while server-only variables are read when a function runs.
In Next.js, a NEXT_PUBLIC_ value is decided once, when Vercel runs next build. A server-only value read inside a route handler, a server action, or a page that renders dynamically is read from the environment at request time. The Next.js docs show reading a value after calling connection() from next/server so the page renders dynamically and the variable is evaluated at runtime.
This has practical consequences:
- A page that is statically generated reads server variables at build time too, so a missing variable can break the build itself. My post "Vercel build failed? The most common Next.js deploy errors and fixes" covers those errors.
- If you need a value that changes without rebuilding, serve it from an API route instead of a NEXT_PUBLIC_ variable.
- Edge runtime functions and middleware have a 5 KB limit per variable at the time of writing, against a 64 KB total per deployment for Node.js and other runtimes, per the Vercel docs. Large certificates or service account JSON belong in Node.js functions.
How do I debug which variable is missing?
Make the app tell you, instead of guessing, by checking for each required variable at startup and failing with a clear message.
My routine:
- Run vercel env ls to list variables per environment, or read the dashboard list row by row.
- Run vercel env pull to download the Development values into .env.local and compare names with your code.
- Search the codebase for process.env and import.meta.env and write down every name used.
- Add a tiny check in a server route that logs whether each required variable is present, as true or false, never the value itself.
- Read the build logs in the Deployments tab. Many SDKs print the missing variable's name during the build.
Never log secret values, even temporarily. Vercel runtime logs are visible to everyone on the team.
A worked example: a quote app with three missing variables
Here is an illustration with a made-up business. A print shop builds a quote request app in Bolt, connects Supabase and Stripe, and deploys to Vercel. The live site shows a blank page and the console says "supabaseUrl is required."
What I find:
- The repo is a Vite app reading import.meta.env.VITE_SUPABASE_URL, but Vercel has SUPABASE_URL. I rename it.
- VITE_SUPABASE_ANON_KEY is ticked for Production only. Preview links fail. I tick Preview too.
- STRIPE_SECRET_KEY was given a VITE_ prefix "to make it work," which exposed it in the bundle. I remove the prefix, move the Stripe call into a server function, and the owner rotates the key in the Stripe dashboard.
- Nobody redeployed after the first two changes, so I redeploy Production and push a commit for Preview.
Thirty minutes of work, plus a key rotation that mattered far more than the blank page.
The bottom line
When environment variables are undefined on Vercel, check in this order: is the variable in Project Settings at all, is it ticked for the environment you are viewing, did you redeploy after adding it, does browser code use the NEXT_PUBLIC_ or VITE_ prefix, and is every reference written as a literal name. Keep secrets unprefixed and on the server. If you have been through the list and it still fails, the problem is usually in how the app builds rather than in the variables, and that is a quick fix for a developer. My Quick Fix starts at $60 for one bug in about two days, and Finish and Launch from $600 covers apps that need several launch problems solved at once.
Questions, answered in one line
Why is my environment variable undefined on Vercel but works locally?
Most often the variable lives only in your local .env.local file, which is not committed to Git, so you need to add it in the Vercel dashboard and redeploy.
Do I need to redeploy after changing environment variables on Vercel?
Yes, Vercel only applies environment variable changes to new deployments, so the running deployment keeps its old values until you redeploy.
Why is my NEXT_PUBLIC_ variable undefined in the browser?
Next.js inlines NEXT_PUBLIC_ values at build time, so the variable must exist when the build runs and must be referenced by its full literal name, not through a dynamic lookup.
What prefix does a Vite or Lovable app need for environment variables?
Vite only exposes variables that start with VITE_ to client code, read through import.meta.env, so a Supabase URL should be named VITE_SUPABASE_URL.
Should secret keys use the NEXT_PUBLIC_ or VITE_ prefix?
No, anything with those prefixes is shipped to every visitor in the JavaScript bundle, so secret keys must stay unprefixed and be read only in server code.
How much does it cost to fix environment variable problems in a deployed app?
Thomas Osayi fixes a single deploy bug like this from $60, and gets apps whose core features already work launched from $600.
Sources
- Environment variables Vercel Docs
- How to use environment variables in Next.js Next.js Docs
- Env Variables and Modes Vite
Related articles
- Supabase auth redirect URL not working? Fix localhost, preview and production redirects
- How to fix a CORS error on a Next.js API route
- Firestore "Missing or insufficient permissions": what triggers it and how to fix it
- Firebase auth/unauthorized-domain: how to fix it on Vercel, previews and custom domains
Fix, finish and launch your AI-built app
Built your app with Lovable, Bolt, v0, Replit or Cursor and got stuck? I fix the bugs, connect auth and payments, clean up the code and deploy it to production.
See the service