v9.4.0 webpack change - How to add custom ENV variables that depends on the buildId?
See original GitHub issueBug report
Describe the bug
Back in <v9.4, I was doing the following to augment my env variables in next.config.js
:
webpack: (config, { isServer, buildId }) => {
const APP_VERSION_RELEASE = `${packageJson.version}_${buildId}`;
// Dynamically add some "env" variables that will be replaced during the build
config.plugins[1].definitions['process.env.APP_RELEASE'] = JSON.stringify(buildId);
config.plugins[1].definitions['process.env.APP_VERSION_RELEASE'] = JSON.stringify(APP_VERSION_RELEASE);
if (isServer) { // Trick to only log once
console.debug(`[webpack] Building release "${APP_VERSION_RELEASE}"`);
}
...
}
I just upgraded to v9.4.0 and now the server crashes with TypeError: Cannot set property 'process.env.APP_RELEASE' of undefined
.
After logging things a bit, I noticed the webpack
function is executed twice, and doesn’t have the same plugins attached between attempts. The result is that once holds DefinePlugin
at index 2
, and the second time, the index is DefinePlugin
.
This is a regression in behaviour, but maybe the way I do things isn’t the recommended one?
To Reproduce
Use v9.4.0
Attempt to write config.plugins[2].definitions['process.env.APP_RELEASE'] = 'test'
in webpack
function
Expected behavior
It should keep the same plugins order between calls.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
v9.4.0 webpack change - How to add custom ENV variables ...
version}_${buildId}`; // Dynamically add some "env" variables that will be replaced during the build config.plugins[1].definitions['process.env.
Read more >Environment Variables - webpack
To disambiguate in your webpack.config.js between development and production builds you may use environment variables.
Read more >Using environment variables with Webpack - Prateek Surana
A guide for setting up and using environment variables with Webpack and handling different values for Production and Development ...
Read more >Environment Variables in Next.js - Akhila Ariyachandra
A guide to working with Environment Variables in Next.js. ... 2020 to reflect the how Next.js v9.4+ handles the .env file.
Read more >Nixpkgs 22.11 manual - NixOS
For allowing the build of a broken package once, you can use an environment variable for a single invocation of the nix tools:...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@timer is @magus’s solution valid here? I’d like to have my unique build ID in my app for analytics and such. We’re trying to trace down some bugs that users have had, but it’s hard to know if they’re solved for certain people when we’re pushing 10x per day.
Thanks!
For anyone discovering this issue and needing a more robust solution, this is working for me.
Before
After