Next.js environment variables undefined on Vercel: NEXT_PUBLIC_, redeploys and "supabaseUrl is required"

By AI App Medic · Updated · Next.js · Vercel · v0 · Supabase

The app runs perfectly with npm run dev. You deploy it to Vercel and something that depends on configuration falls over. Common symptoms:

Error: supabaseUrl is required.

@supabase/ssr: Your project's URL and API key are required to create a Supabase client!

TypeError: Failed to construct 'URL': Invalid URL

console.log(process.env.NEXT_PUBLIC_API_URL)  // undefined

Sometimes the build fails during "Collecting page data" or "Generating static pages" with one of these errors. Sometimes the build passes and the browser throws instead. Either way, a value the app needs is not there when it runs.

This comes up constantly with projects from v0, Cursor and Bolt, because the AI tool puts keys in .env.local, the app works on your machine, and nobody tells you that the file stays behind. Next.js handles environment variables in two different ways, and Vercel adds a few rules of its own. Go through the causes below in order.

Cause 1: the variables only exist in .env.local

Next.js loads .env.local during development and builds on your machine. The default .gitignore excludes it, which is correct, since it often holds secrets. So when Vercel builds from your Git repository, none of those values exist.

Open the project in Vercel, go to Settings, Environment Variables, and add each name and value. Copy the names exactly, including the prefix and the case. Paste values without surrounding quotes. Or do it from the CLI:

npx vercel link
npx vercel env add NEXT_PUBLIC_SUPABASE_URL production
npx vercel env add NEXT_PUBLIC_SUPABASE_ANON_KEY production
npx vercel env ls

To go the other way and get the Vercel values onto your machine, run npx vercel env pull .env.local. That keeps local and deployed settings from drifting apart.

Cause 2: the name has no NEXT_PUBLIC_ prefix

According to the Next.js documentation, variables without the NEXT_PUBLIC_ prefix are only available in the Node.js environment, which means server components, route handlers, server actions and the build itself. A component marked "use client" runs in the browser, and there process.env.SUPABASE_URL is simply undefined.

For values that are meant to be public, such as the Supabase project URL and the anon or publishable key, rename them with the prefix in both the code and Vercel:

// lib/supabase/client.ts
import { createBrowserClient } from "@supabase/ssr";

export function createClient() {
  return createBrowserClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL!,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
  );
}

Do not do this for secrets. A Stripe secret key, an OpenAI key or the Supabase service role key must never get the prefix, because Next.js copies every NEXT_PUBLIC_ value into JavaScript that anyone can download. If the AI "fixed" an undefined secret by adding NEXT_PUBLIC_, undo it, move the call into a route handler, and rotate the key. The API key exposed in the bundle and service role key exposed guides explain how to check and clean up.

// app/api/summary/route.ts  (server only, secret stays unprefixed)
export async function POST(req: Request) {
  const key = process.env.OPENAI_API_KEY;
  if (!key) {
    return Response.json({ error: "OPENAI_API_KEY is not set" }, { status: 500 });
  }
  // call the API with key here
  return Response.json({ ok: true });
}

Cause 3: you did not redeploy

Vercel's documentation is direct about this: environment variable changes are not applied to previous deployments, only to new ones. On top of that, Next.js replaces each process.env.NEXT_PUBLIC_... reference with the literal value during next build. After the build, the value is frozen into the files.

After adding or changing a variable, open Deployments, click the three dot menu on the latest deployment and choose Redeploy, or push a new commit. Then test the new deployment's URL, not a tab you had open from before. If you suspect a cached build, redeploy with "Use existing Build Cache" turned off.

Cause 4: the variable is set for the wrong environment

Each variable on Vercel is enabled for Production, Preview, Development or a mix. A variable saved only for Production is missing in every preview deployment, including the ones created for pull requests and branch pushes. That is why "it works on the main domain but not on the preview link" is so common.

Open the variable, check which environments are ticked, and add the ones you need. Use separate values per environment when they should differ, for example a staging Supabase project for Preview. Then redeploy the environment you changed.

Cause 5: the code reads the variable dynamically

Next.js only replaces a reference it can see literally in the code. The Next.js documentation gives these as examples that will not be inlined:

// NOT replaced in the browser bundle
const name = "NEXT_PUBLIC_SUPABASE_URL";
const url = process.env[name];

const env = process.env;
const url2 = env.NEXT_PUBLIC_SUPABASE_URL;

const { NEXT_PUBLIC_SUPABASE_URL } = process.env;

// replaced: write the full name every time
const url3 = process.env.NEXT_PUBLIC_SUPABASE_URL;

AI tools like to write config helpers that loop over a list of names or destructure process.env. Those helpers work on the server and return undefined in the browser. Rewrite them so each public variable is read with its full literal name.

Cause 6: monorepos and build caches

If the Next.js app lives in a subfolder such as apps/web, set the Root Directory in Vercel to that folder, and add the variables to that Vercel project. With Turborepo, Vercel's docs say to list environment variables in turbo.json, so a cached build made with different values is not reused:

{
  "$schema": "https://turborepo.com/schema.json",
  "tasks": {
    "build": {
      "env": ["NEXT_PUBLIC_SUPABASE_URL", "NEXT_PUBLIC_SUPABASE_ANON_KEY"],
      "outputs": [".next/**", "!.next/cache/**"]
    }
  }
}

Make the build fail when a variable is missing

The worst version of this bug is a build that passes and a site that breaks for users. Add a check to next.config.ts. This file runs in Node during the build, so reading process.env[name] here is fine:

import type { NextConfig } from "next";

const required = ["NEXT_PUBLIC_SUPABASE_URL", "NEXT_PUBLIC_SUPABASE_ANON_KEY"];
for (const name of required) {
  if (!process.env[name]) {
    throw new Error(`Missing environment variable ${name}`);
  }
}

const nextConfig: NextConfig = {};
export default nextConfig;

Now a missing variable stops the deployment with a clear message in the build log, and the last working deployment stays live. If the build fails for other reasons, such as type errors, see Cursor app Vercel build failed.

Prevention

Commit a .env.example file that lists every variable name with no values, so anyone deploying knows what to set. Decide for each variable whether it is public or secret before you name it, and only give public ones the NEXT_PUBLIC_ prefix. Keep Vercel as the source of truth and pull it locally with vercel env pull. Keep the build check above in place. And after every change in the Vercel settings, redeploy and test the new URL.

FAQ

Why are my environment variables undefined on Vercel but fine locally?

Locally Next.js reads .env.local, which is listed in .gitignore and never reaches Vercel. Add the same variables in the Vercel project settings, check that they are enabled for the environment you are viewing, and redeploy.

I added the variable on Vercel and it is still undefined. Why?

Vercel applies environment variable changes only to new deployments. The deployment you are looking at was built with the old set. Open Deployments, redeploy the latest one, and test the new URL.

Can I read a NEXT_PUBLIC_ variable at runtime without rebuilding?

Not in client code. Next.js replaces process.env.NEXT_PUBLIC_ references with the literal value during next build. If a value must change without a rebuild, read it on the server, for example in a server component or route handler, and pass it down.

Should I prefix my Stripe or OpenAI secret key with NEXT_PUBLIC_ to make it work?

No. Anything with NEXT_PUBLIC_ is copied into JavaScript that every visitor downloads. Keep secrets unprefixed and use them only in server components, route handlers or server actions.

What does supabaseUrl is required mean?

createClient from supabase-js was called with an undefined URL. In a Next.js app on Vercel that nearly always means NEXT_PUBLIC_SUPABASE_URL is missing from the Vercel project for that environment, or the app was not redeployed after it was added.

Related guides