Skip to content
>_VibeUnstuck

process.env.NEXT_PUBLIC_ is undefined

Last verified:

The fix

process.env.NEXT_PUBLIC_ is undefined. The name is missing from .env.local, it has no NEXT_PUBLIC_ prefix, or Next.js has not been restarted since you added it. Do not put a secret on that prefix.

  1. 1

    Match the name in the code to a line in .env.local

    Client code in Next.js must read process.env.NEXT_PUBLIC_NAME. Server code can read process.env.NAME and should, for secrets. A dynamic lookup such as process.env[name] is not inlined. In a Vite app (Lovable, Bolt), the client prefix is VITE_, not NEXT_PUBLIC_. The value that belongs in the browser for Supabase is the project URL and the anon or publishable key.

    NEXT_PUBLIC_SUPABASE_URL=https://<project-ref>.supabase.co
    NEXT_PUBLIC_SUPABASE_ANON_KEY=<anon-or-publishable-key>
  2. 2

    Restart or rebuild

    Stop next dev and start it again after any .env.local change. On Vercel, add the variable for the right environment and redeploy. The previous deployment keeps the old inlined value. An empty string is set and will not throw. Log whether the value is missing or blank before you add another prefix.

  3. 3

    Check for quotes and spaces

    Some env loaders include wrapping quotes in the value. If the URL looks like "https://..." including the quote characters, remove the quotes in .env.local or the host dashboard. A trailing space counts as part of the value.

  4. 4

    Do not prefix a secret to make it defined

    If the value is a service role key, an sb_secret_ key, an sk_ key, or any other secret, it is undefined in the browser on purpose. Read it from a server route under a name with no public prefix. Adding NEXT_PUBLIC_ so the client can see it publishes the key. That case has its own page, linked below.

Why it happens

Next.js inlines process.env.NEXT_PUBLIC_* into the client bundle when it builds. A name in .env.local without the NEXT_PUBLIC_ prefix is visible on the server and deliberately undefined in client code. A line you add after next dev is already running stays undefined until you stop the server and start it again.

On Vercel, the value has to exist for the environment you deployed, before the build. Changing it later does nothing until the next build. Vite works the same way with the VITE_ prefix: only those names are exposed to the browser, and they are public. Putting a service role key or any other secret on NEXT_PUBLIC_ makes the value defined by copying it into the JavaScript the browser downloads.

The next error you'll probably hit

Once this is fixed, the usual follow-up is NEXT_PUBLIC_ secret in the bundle. A NEXT_PUBLIC_ or VITE_ secret is in the browser bundle. Rotate the key, move the call to a server route, and keep real public keys in the client.

Prompt to paste back into your AI tool

Paste this above the error.

process.env.NEXT_PUBLIC_NAME is undefined in the browser. It may be in .env.local under a different name. On Vite this may be a VITE_ variable instead.

Do not put a secret behind NEXT_PUBLIC_ or VITE_. Do not hardcode the value in source. Do not prefix a service_role, sb_secret_, or sk_ key to make this error go away.

1. If this value is safe to ship to the browser, use the correct prefix (NEXT_PUBLIC_ for Next, VITE_ for Vite) and tell me to restart the dev server or redeploy.
2. If it is a secret, move the call to a server route that reads an unprefixed variable, and remove the public prefix from .env.local.
3. If the name is already correct, tell me to restart next dev. A line added while the server was running is still undefined.
4. Change only the read site and the server route if one is required.

Common questions

+It is in .env.local and process.env.NEXT_PUBLIC_ is still undefined. Why?

The dev server was not restarted, the name lacks NEXT_PUBLIC_, or the client bundle was built before the line existed. Restart. On Vercel, saving the variable does not change a deployment that already built. For Vite, the name has to start with VITE_.

+Why can the server log the value while the client cannot?

Unprefixed variables are server-only. That is the point of the prefix. Keep secrets on that side. Adding NEXT_PUBLIC_ to a secret makes the client value defined by putting the secret in the bundle.

+Can I put the Supabase service role key in NEXT_PUBLIC_ so createClient stops throwing?

No. The browser client uses the anon or publishable key. The service role key stays in an unprefixed variable and is read on the server. A missing public URL or anon key is supabaseUrl is required, which is a different page.

Related errors