Description
Link to the code that reproduces this issue
https://github.com/hood/nextjs-env-repro
To Reproduce
- Build the application using the
build
command. - Run the output with
node .next/standalone/server.js
.
Current vs. Expected behavior
Client components can’t seem to access environment variables that I’m 100% sure are defined (other components can access them just fine). I’d expect them to be able to access NEXT_PUBLIC_*
environment variables.
Let’s refer to my MOCK_ENV_VAR
environment variable:
- I’ve set it in the environment where the process is running.
- I’ve set both
MOCK_ENV_VAR
andNEXT_PUBLIC_MOCK_ENV_VAR
in an.env.production
file which is correctly getting consumed. - Component
Child
is not able to access the environment variable at all, seeing MOCK_ENV_VAR: undefined, NEXT_PUBLIC_MOCK_ENV_VAR: undefined.
I’m using app router, deploying my Next.js application as a standalone server, built with the following command: next build --experimental-build-mode compile
, with all caching mechanisms disabled (export const dynamic = "force-dynamic”;
on all pages). I basically build all my pages afresh on every hit.
This issue only seems to affect my client components, server components are unaffected.
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000
Available memory (MB): 32768
Available CPU cores: 10
Binaries:
Node: 24.1.0
npm: 11.3.0
Yarn: 1.22.22
pnpm: 10.11.0
Relevant Packages:
next: 15.3.3 // Latest available version is detected (15.3.3).
eslint-config-next: N/A
react: 19.1.0
react-dom: 19.1.0
typescript: 5.8.3
Next.js Config:
output: standalone
Which area(s) are affected? (Select all that apply)
Dynamic Routes, Output
Which stage(s) are affected? (Select all that apply)
Other (Deployed)
Additional context
An interesting thing is that I see the NEXT_PUBLIC_*
environment variables consumed by my client components as they should be set under the window
global, not inlined as the docs say:
(Excerpt from the output bundle)
"p",
{
children: "Called by: "
.concat("child", " -> MOCK_ENV_VAR=")
.concat(n.env.MOCK_ENV_VAR, " | NEXT_PUBLIC_MOCK_ENV_VAR=")
.concat(window.NEXT_PUBLIC_MOCK_ENV_VAR),
};
I’ve done a quick check rolling Next.js back to 15.2, and the NEXT_PUBLIC_
-prefixed environment variables are back working correctly in my client code.