question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v9.4.0 webpack change - How to add custom ENV variables that depends on the buildId?

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
nandorojocommented, Jan 27, 2021

@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!

2reactions
maguscommented, Jan 23, 2021

For anyone discovering this issue and needing a more robust solution, this is working for me.

Before

config.plugins.push(
  new webpack.DefinePlugin({
    __DEV__: JSON.stringify(EnvConfig.DEV),
    'process.env.SENTRY_RELEASE': JSON.stringify(buildId),
  }),
);

After

config.plugins.forEach((plugin) => {
  if (plugin.constructor.name === 'DefinePlugin') {
    plugin.definitions = {
      ...plugin.definitions,
      __DEV__: JSON.stringify(EnvConfig.DEV),
      'process.env.SENTRY_RELEASE': JSON.stringify(buildId),
    };
  }
});
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found