Next.js · silent bug
NEXT_PUBLIC_ env var is undefined in production (or stuck on the old value)
verified on · Next.js 16.4.5 · React 19.3
Check yours: npm ls next react · On a different version? Paste your package.json into the free Doctor and it checks your versions against these fixes.
console.log(process.env.NEXT_PUBLIC_API_URL)
// undefined
Works with npm run dev. On the deployed site it's undefined, or still the old value after you changed it in the hosting dashboard.What it means
Next.js copies NEXT_PUBLIC_ values into the JavaScript bundle when next build runs. The browser never reads your host's env vars. If the variable wasn't set where the build ran, undefined is baked in, and changing it later does nothing until the app is built again. In our Next.js 16.4.5 test, server code kept the build time value of a NEXT_PUBLIC_ variable too, while a variable without the prefix was read fresh at request time on a dynamic page.
Common causes
- The value only lives in .env.local, which isn't committed, so the build on Vercel, Netlify or CI never saw it
- It was added or changed in the hosting dashboard after the last build, and nothing was redeployed
- It's set for Preview but not Production (or the other way round) in Vercel's environment settings
- Docker: it's passed with docker run -e at runtime, but next build ran during docker build, where it wasn't set
- The name has no NEXT_PUBLIC_ prefix, so it's server only and undefined in the browser
- One build or image is promoted from staging to production, so the public value is frozen from whichever environment built it
How to fix it
- Add the variable in your host's settings for the environment that builds (Production, Preview or both)
- Start a new deployment so next build runs again with the value. Changing it in the dashboard doesn't touch a build that already exists
- Write it out in full: process.env.NEXT_PUBLIC_API_URL. Don't destructure it: const { NEXT_PUBLIC_API_URL } = process.env came back undefined in the browser in our test with both Turbopack and webpack (npx github:vibeboot/vibeboot-doctor flags it in your repo). The Next.js docs say process.env[name] and const env = process.env lookups are not inlined either. In our test process.env[name] only worked with Turbopack, so don't count on either
- Docker: declare it in the Dockerfile with ARG and then ENV, both before RUN npm run build, and pass the value with --build-arg when you run docker build
- If the value must change without a rebuild, drop the prefix: read a plain variable in a Server Component that renders per request (await connection() first) and pass it down as a prop
- Never put a secret in a NEXT_PUBLIC_ variable. Everything with that prefix ships to every visitor
import { connection } from "next/server";
import Widget from "./widget";
// Read per request: a new value needs
// a restart, not a rebuild.
export default async function Page() {
await connection();
return <Widget apiUrl={process.env.API_URL} />;
}Prompt for Cursor or Claude Code
A NEXT_PUBLIC_ environment variable is undefined (or stale) in production but works locally. Find every place it's read and make sure each one is written as process.env.NEXT_PUBLIC_NAME in full (no process.env[name], no destructuring). Tell me exactly where it has to be set so it exists when next build runs on my host, and for which environment, then remind me to redeploy. If the value must change without a rebuild, or is a secret, move it to a server only variable read in a dynamically rendered Server Component and pass it down as a prop. Don't add fallback values that hide a missing variable.Not quite your error? Paste yours into the free VibeBoot Doctor or browse the Next.js error fixer.
Check your whole repo from the terminal: npx github:vibeboot/vibeboot-doctor (free, offline).
Upgrading from Next.js 15? The Next.js 16 upgrade checklist covers every breaking change, the error it causes and the fix.
next: skip the next one
Skip the setup. The VibeBoot SaaS Starter gives you auth, Stripe and a database already wired on Next.js 16, with AGENTS.md and .cursor/rules that keep your agent on the Next.js 16 conventions. $49, one-time.
14-day money-back guarantee · lifetime updates
Other Next.js errors
- useState / useEffect in a Server Component
- Event handlers cannot be passed to Client Component props
- Functions cannot be passed directly to Client Components
- Only plain objects can be passed to Client Components
- "use client" must be placed before other expressions
- export "metadata" from a "use client" page
$ fixed it? get the next one first
New fixes for what AI agents break, by email.
When we add a fix or a Cursor rule for a new Next.js, Auth.js, Prisma or Stripe break, it goes out in the VibeBoot newsletter. Free, occasional, unsubscribe in one click.