Error: NEXT_PUBLIC_ environment variable is undefined in production but works locally
Quick answer
`NEXT_PUBLIC_` variables are inlined into your JavaScript bundle at build time, not read at runtime. If you added the variable after your last deploy, or only to certain environments, you need to trigger a fresh build for it to take effect.
What causes this error
Unlike server-only environment variables, anything prefixed NEXT_PUBLIC_ is baked directly into the client JavaScript bundle during `next build` — it is not read live from the server's environment at request time. If the variable wasn't present at build time, it will be `undefined` in the browser no matter what you set afterward.
The Fix
- Confirm the variable is set in your hosting provider's dashboard for the correct environment
- Trigger a completely new build/deploy — don't rely on redeploying a cached build
- Double-check the exact spelling — `NEXT_PUBLIC_` is case-sensitive and must match exactly in both the dashboard and your code
Common causes / variations
- Adding the env var after the last deploy, without triggering a rebuild
- Using a plain (non-`NEXT_PUBLIC_`) variable in client-side code, where it's never exposed to the browser by design
- Local `.env.local` has the variable but it was never added to the hosting provider's dashboard
Related errors
See also: Vercel Environment Variable not found.
Was this fix helpful?
Comments
Loading comments...