Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--node-env doesn't set the mode as expected #3992

Closed
mgnsm opened this issue Oct 31, 2023 · 3 comments · May be fixed by webpack/webpack.js.org#7080
Closed

--node-env doesn't set the mode as expected #3992

mgnsm opened this issue Oct 31, 2023 · 3 comments · May be fixed by webpack/webpack.js.org#7080
Assignees
Labels

Comments

@mgnsm
Copy link

mgnsm commented Oct 31, 2023

According to the docs and this comment I should be able to use the --node-env option to set both process.env.NODE_ENV and mode but I cannot get this to work as expected.

Using the following webpack.config.js configuration:

const config = {};

module.exports = (env, argv) => {
  console.log("mode is " + argv.mode);
  console.log("process.env.NODE_ENV is " + process.env.NODE_ENV);

  return config;
};

...and the following package.json file:

{
  "name": "WebpackIssue",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "webpack": "^5.89.0",
    "webpack-cli": "^5.1.4"
  },
  "scripts": {
    "build": "webpack build --node-env=production"
  }
}

Running the npm run build command outputs the following:

mode is undefined
process.env.NODE_ENV is production

Why is argv.mode undefined here? Do I still need to set the mode explicitly after all (like below) or what am I doing wrong?

webpack build --mode=production --node-env=production
@mgnsm mgnsm added the Bug label Oct 31, 2023
@evenstensberg

This comment was marked as outdated.

@evenstensberg
Copy link
Member

Thanks for the issue. It is fixed in #4009

@alexander-akait
Copy link
Member

I want to say it works as expected, argv contains arguments which you provided as CLI arguments, i.e. you pass only --node-env, so just use argv.nodeEnv to get value, we set the mode option after loading configuration, also you can use process.env.NODE_ENV because we set it before loading configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants