next.js: Regression: Since 15.3 environment variables are not available in client components using standalone output mode
Link to the code that reproduces this issue
https://github.com/hood/nextjs-env-repro
To Reproduce
- Build the application using the
buildcommand. - 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_VARandNEXT_PUBLIC_MOCK_ENV_VARin an.env.productionfile which is correctly getting consumed. - Component
Childis 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.
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 2
- Comments: 18
@hood sorry about that, yeah, it’s a different issue than what I first met. Already deleted my comments. Now that I already get my hands dirty, will try to look for a fix for this issue if I could.
@laishere with usual build everything will probably work. but we are talking about creating build with command
next build --experimental-build-mode=compile@Moon04 go to 15.2.* 🥲 that’s what I decided to do
@ijjk https://github.com/vercel/next.js/pull/76990 This MR may have introduced the issue, but I don’t get where it stemmed from so I don’t get if it was an inteded change or not.
I’ve tried running
next build --experimental-build-mode=compile && next build --experimental-build-mode=generate(at least this is what I figured out was the new intended way), butgeneratebreaks trying to fetch data for mysitemap.ts, whereas I was using the command to avoid data fetching completely during my builds.I’ve tried running
generate-envinstead ofgenerate, which successfully completes the build, but I then get errors like this breaking my pages, caused by the output code getting 404s when trying to fetch some chunks:Given that I am navigating the darkness while attempting to reconstruct the correct procedure from limited comments in GitHub discussions, could you kindly provide a recommended method for successfully getting this setup to work again?