简体   繁体   English

webpack 警告 - DefinePlugin 中的警告“process.env.NODE_ENV”的值冲突

[英]webpack warning - WARNING in DefinePlugin Conflicting values for 'process.env.NODE_ENV'

I'm getting the warning in the title when I try to run development mode.当我尝试运行开发模式时,我在标题中收到警告。 This script used to work fine for an earlier website but now I always get this warning.该脚本过去在较早的网站上运行良好,但现在我总是收到此警告。

This is my package.json:这是我的 package.json:

{
  "name": "frontend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development --watch",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.13.10",
    "@babel/preset-env": "^7.13.12",
    "@babel/preset-react": "^7.12.13",
    "babel-loader": "^8.2.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "webpack": "^5.27.2",
    "webpack-cli": "^4.5.0"
  },
  "dependencies": {
    "@babel/plugin-proposal-class-properties": "^7.13.0",
    "@material-ui/core": "^4.11.3",
    "@material-ui/icons": "^4.11.2",
    "react-router-dom": "^5.2.0"
  }
}

And my webpack.config.js:还有我的 webpack.config.js:

const path = require("path");
const webpack = require("webpack");

module.exports = {
  entry: "./src/index.js",
  output: {
    path: path.resolve(__dirname, "./static/frontend"),
    filename: "[name].js",
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
        },
      },
    ],
  },
  optimization: {
    minimize: true,
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env": {
        // This has effect on the react lib size
        NODE_ENV: JSON.stringify("production"),
      },
    }),
  ],
};

I've searched around everywhere but couldn't find anything similar to this warning.我到处搜索,但找不到与此警告类似的内容。

try changing尝试改变

new webpack.DefinePlugin({
      "process.env": {
        // This has effect on the react lib size
        NODE_ENV: JSON.stringify("production"),
      },
    }),

to

plugins: [
    new webpack.DefinePlugin({
        'process.env.NODE_ENV' : JSON.stringify('production')
    })
]

Thanks for helping everyone, very much appreciated!感谢大家的帮助,非常感谢!

I ended up replacing "production" with "development" in the following snippet of the webpack.config:我最终在 webpack.config 的以下片段中将“生产”替换为“开发”:

plugins: [
    new webpack.DefinePlugin({
      "process.env": {
        // This has effect on the react lib size
        NODE_ENV: JSON.stringify("development"),
      },
    }),
  ]

It got rid of the warning but I'm wondering what impact this has.它摆脱了警告,但我想知道这有什么影响。

You are saying you "try to run development mode".您是说您“尝试运行开发模式”。 According to your package.json that means running:根据您的package.json这意味着运行:

webpack --mode development --watch (NOTE: v4 syntax) webpack --mode development --watch (注意:v4语法)

  1. That mode parameter already set webpack's own mode , that is the mode to be used when webpack is running, meaning during " build time ", or "compile time" if you will.mode参数已经设置了 webpack 自己的mode ,即 webpack 运行时使用的模式,意思是“构建时间”或“编译时间”,如果你愿意的话。
  2. The DefinePlugin checks that variable when you try to define it for your " run time " (which is different from " build time "), and thus warns you if the two are different ( code here ).当您尝试为“运行时间”(不同于“构建时间”)定义该变量时, DefinePlugin检查该变量,因此如果两者不同( 此处的代码)会发出警告。

Soloution : Make sure, mode in your webpack.config object (or in the CLI command, which overrides it in the config object) is the same as the one you pass to the DefinePlugin .解决方案:确保webpack.config object 中的mode (或在配置对象中覆盖它的 CLI 命令中)与传递给DefinePlugin的模式相同。

How to access webpack CLI parameters in webpack.config.js ?如何访问 webpack.config.js 中的webpack.config.js CLI 参数?

If you want to be able to provide the mode parameter from CLI or a package.json script, and still have the DefinePlugin not give a warning (meaning you get that value from the CLI and plug that into the DefinePlugin ), do this:如果您希望能够从 CLI 或package.json脚本提供mode参数,并且DefinePlugin仍然没有给出警告(意味着您从 CLI 获取该值并将其插入到DefinePlugin ),请执行以下操作:

Change the contents of your webpack.config.js from your module.exports = { mode: ..., rules: ... };从你的module.exports = { mode: ..., rules: ... };更改webpack.config.js的内容。 (or export default {... } ) config object to a function as shown below. (或export default {... } )将object配置为function ,如下所示。 This function works exactly the same (you return the final config object), but allows reading environment variables from the first parameter env , as well as webpack CLI options from the second argument argv :这个 function 的工作原理完全相同(您return最终配置对象),但允许从第一个参数env读取环境变量,以及从第二个参数argv读取 webpack CLI 选项

module.exports = (env, argv) => {
  // `mode` is `'XX'` if you ran webpack like so: `webpack watch --mode XX` (v5 syntax)
  const mode = argv.mode || 'development'; // dev mode by default

  // ...

  return {
    mode,   // this is optional, since webpack already knows the `mode` from the CLI parameter
    // ...
    plugins: [
      // ...
      new webpack.DefinePlugin({
        'process.env.NODE_ENV': JSON.stringify(mode)
      })
    ]
    // ...
  };
};

PS : You are using Webpack@4. PS :您正在使用 Webpack@4。 If you used Webpack@5, you would have to change webpack --mode development --watch to webpack watch --mode development .如果您使用 Webpack@5,则必须将webpack --mode development --watch更改为webpack watch --mode development

PPS : Always prefer 'process.env.NODE_ENV': JSON.stringify('X') over 'process.env': JSON.stringify({ NODE_ENV: 'X' }) , since the former safely replaces all occurrences of process.env.NODE_ENV in your code with "X" , while the latter replaces any occurrence of process.env with { "NODE_ENV": "X" } . PPS :总是更喜欢'process.env.NODE_ENV': JSON.stringify('X')而不是'process.env': JSON.stringify({ NODE_ENV: 'X' }) ,因为前者安全地替换了所有出现的process.env.NODE_ENV在您的代码中使用"X" ,而后者将任何出现的process.env替换为{ "NODE_ENV": "X" } That in turn is likely going to mess up other environment variables.反过来,这可能会弄乱其他环境变量。 Example: process.env.Y will become { "NODE_ENV": "X" }.Y which is undefined .示例: process.env.Y将变为{ "NODE_ENV": "X" }.Y这是undefined

This error means that you tried to reassign process.env.NODE_ENV in DefinePlugin call with the value different from it has before.此错误意味着您尝试在DefinePlugin调用中重新分配process.env.NODE_ENV ,其值与以前不同。

You can set it implicitly by adding mode option to config, or adding nodeEnv key to optimization .您可以通过将mode选项添加到 config 或将nodeEnv键添加到optimization来隐式设置它。 Or just set the environment variable before you run webpack.或者在运行 webpack 之前设置环境变量。

And it looks like that is your case.看起来这就是你的情况。 You set NODE_ENV=development somewhere, ran webpack, and then you are trying to reassign it to production .您在某处设置NODE_ENV=development ,运行 webpack,然后您尝试将其重新分配给production

Check how do you run webpack and NODE_ENV environment value.检查如何运行 webpack 和 NODE_ENV 环境值。

Just asnwered here: https://stackoverflow.com/a/70141040/7841170刚刚在这里: https://stackoverflow.com/a/70141040/7841170

In my case it was wrong path.就我而言,这是错误的路径。

I ran the command from git Bash with wrong camelCase letters.我从 git Bash 用错误的驼峰式字母运行命令。

like: c://Dev/Product喜欢: c://Dev/Product

instead of: c://dev/Product而不是: c://dev/Product

Fixed the path and it works.修复了路径,它可以工作。

The value in  NODE_ENV: JSON.stringify("this_value_here") 
should match the value inside package.json "scripts": {
    "dev": "webpack --mode development --watch",
    "build": "webpack --mode production"
  }, 

If you are running "npm run dev" , keep the JSON.stringify("development") , if you are running "npm run build" ,change it to JSON.stringify("production") .如果您正在运行"npm run dev" ,请保留JSON.stringify("development") ,如果您正在运行"npm run build" ,请将其更改为JSON.stringify("production") Basically, match it to the type of mode you are running.基本上,将其与您正在运行的模式类型相匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM