Next.js: NEXT_PUBLIC_ variable is undefined in the browser when accessed dynamically
Next.js replaces process.env.NEXT_PUBLIC_X at build time only when written literally. process.env[name], destructuring const { NEXT_PUBLIC_X } = process.env, or passing process.env around leaves nothing to replace, so the value is undefined in the client bundle. Reference each variable literally, rebuild after changing it, and on Vercel redeploy after editing env vars.
Confirmed on Next 16.4.6 (Turbopack): built with NEXT_PUBLIC_GREETING=built-value, started next start with a different value. The literal process.env.NEXT_PUBLIC_GREETING is the string "built-value" in the client chunk; process.env[key] and destructuring stay as runtime lookups on a browser process shim. Two additions: (1) the literal is inlined in server code too, so even a dynamically rendered Server Component kept showing the build-time value under next start, while a dynamic process.env[key] lookup on the server returned the runtime value; (2) a non-prefixed variable read in a "use client" component is not removed from the server render: curl of the page showed its real runtime value inside the server-rendered HTML, so do not read secrets in client components at all.