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

Clarify interaction between --node-env and mode #7080

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/content/api/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ You can use `--node-env` option to set `process.env.NODE_ENV`, which is availabl
npx webpack --node-env production # process.env.NODE_ENV = 'production'
```

T> The `mode` option would respect the `--node-env` option if you don't set it explicitly, i.e. `--node-env production` would set both `process.env.NODE_ENV` and `mode` to `'production'`
T> When the `mode` option is not set in the configuration, the `--node-env` option can be used to set `mode`. For example, `--node-env production` would set both `process.env.NODE_ENV` and `mode` to `'production'`. If the configuration [exports a function](/configuration/configuration-types/#exporting-a-function), the `--node-env` value is assigned to `mode` after the function returns, so it will not be set in the function arguments (`env` and `argv`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify

so it will not be set in the function arguments (env and argv).

Because

const path = require("path");

module.exports = function test(env, argv) {
  console.log(argv);

  return {
    entry: "./src/index.js",
    devtool: false,
    cache: {
      type: "filesystem",
    },
    output: {
      path: path.resolve(__dirname, "./dist"),
      filename: "main.js",
    },
  };
};

Run

webpack --node-env production

Output:

{
  nodeEnv: 'production',
  env: { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or let's do it better:

so the mode option will not be set in the function arguments (env and argv). You need to use the argv.nodeEnv option


### define-process-env-node-env

Expand Down