Difference between global.process.env
and process.env
#82272
-
SummaryHow is that possible that running a production build from Next.js I have the following output on a dynamically rendered page?
Scripts in the package.json: "build": "next build",
"start": "NEXT_PUBLIC_TEST=from_proc next start" .env file: NEXT_PUBLIC_TEST=from env file So, I'm wondering how is that possible that Why is that nuance not documented? Additional informationNext.js 15 App Router Example |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This one is a bit tricky to understand, but basically the Webpack DefinePlugin is used to replace NEXT_PUBLIC_ prefixes env vars with their value during build time, into the source code. This depends on the exact As you can image, this is rather sensitive, and something like See this documentation from that DefinePlugin plugin: Something like this they document over at that plugin: new webpack.DefinePlugin({
'typeof window': JSON.stringify('object'),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'process.env.NEXT_PUBLIC_FOO': JSON.stringify(process.env.NEXT_PUBLIC_FOO), // this bit@
}); I can never find where this is done in Next.js but I'll do a bit of search - anyway, you can imagine that something like this is done to replace I think it is here, that the expressions to replace with the DefinePlugin are defined: next.js/packages/next/src/lib/static-env.ts Lines 17 to 28 in 531e938 I checked if Turbopack builds also do this, and yeah - the Turbopack replacement is also expecting strictly |
Beta Was this translation helpful? Give feedback.
This one is a bit tricky to understand, but basically the Webpack DefinePlugin is used to replace NEXT_PUBLIC_ prefixes env vars with their value during build time, into the source code. This depends on the exact
process.env.NEXT_PUBLIC_
declaration. It is a bit of magic, coming from the ability to statically analyze this declaration.As you can image, this is rather sensitive, and something like
global.process.env.NEXT_PUBLIC_
falls out and is considered to be dynamic access.See this documentation from that DefinePlugin plugin:
Something like this they document over at that plugin: