Next.js env var undefined in the browser: missing NEXT_PUBLIC_
Next.js only exposes environment variables prefixed with NEXT_PUBLIC_ to the browser; others exist only on the server. Rename public values to NEXT_PUBLIC_*, set them on your host, and rebuild, because they're inlined at build time.
Cause: A client component reads a variable without the NEXT_PUBLIC_ prefix, or it was added after the build.
How to fix it
- Rename public values (e.g. Supabase URL, anon key) to
NEXT_PUBLIC_*. - Set them in your host's env settings for the right environment.
- Redeploy:
NEXT_PUBLIC_values are inlined at build time. - Never prefix secrets with
NEXT_PUBLIC_.
Example
Illustrative example, not from a customer project.
- SUPABASE_URL=https://xyz.supabase.co
+ NEXT_PUBLIC_SUPABASE_URL=https://xyz.supabase.co
FAQ
It works in server code but not in a component.
Server code can read all env vars; the browser only gets NEXT_PUBLIC_ ones.
I changed the value but nothing changed.
Rebuild; the old value is baked into the bundle.
QUICK FIX · $49 · THIS ERROR ONLY
Get this exact error fixed — $49 Quick FixFixed within scope, or we keep working until it is · Read-only access Not sure it's this error? $149 diagnosis
- Fixed, or we keep working free until it is (within the written scope).
- A human reviews every fix before it ships.
- Read-only access; fixes arrive as pull requests you merge.
- Your $149 diagnosis is credited in full toward any paid job.
One error, one root cause, fixed in your app. If it turns out bigger, we tell you before doing more, and your $49 is credited toward the fix, diagnosis or Launch Path if you book within 14 days. Fixing three? The 3-Error Pack is $119.
Bigger job: Getting the whole app live? Launch Path, $1,195 flat.
Built with Lovable, Bolt, Replit, Cursor or v0? See rescue by platform and the security checklist.