简体   繁体   English

webpack DefinePlugin process.env.NODE_ENV 未定义

[英]webpack DefinePlugin process.env.NODE_ENV is undefined

I am not too good with front-end technologies... So if I have wrong expectations - please correct me or my code.我不太擅长前端技术......所以如果我有错误的期望 - 请纠正我或我的代码。 I have created a repository with code that allows to reproduce issue.我创建了一个包含允许重现问题的代码的存储库。 Here is the link: https://github.com/ffatheranderson/webpack-issue-reproduction这是链接: https://github.com/ffatheranderson/webpack-issue-reproduction

as described in readme.md of the project:如项目的 readme.md 中所述:

======================================== =========================================

  • What I expect?我期待什么? - I expect that after I execute npm run watch command - the generated result/bundle.js file to have such lines: - 我希望在我执行npm run watch命令后 - 生成的result/bundle.js文件有这样的行:
...
var _environment = 'development';
var _ANOTHER_VARIABLE = "another variable value";
...
  • What is actual result?实际结果是什么? - after I execute npm run watch command - the generated result/bundle.js file contains such lines: - 在我执行npm run watch命令后 - 生成的result/bundle.js文件包含这样的行:
...
var _environment = undefined;
var _ANOTHER_VARIABLE = "another variable value";
...
  • Why do I have such expectations?为什么我会有这样的期望? - because of these lines: - 因为这些行:
...
  plugins: [
        new webpack.DefinePlugin({
            ENVIRONMENT: JSON.stringify(process.env.NODE_ENV),
            ANOTHER_VARIABLE: JSON.stringify("another variable value"),
        })
    ]
...

in webpack.config.js file.webpack.config.js文件中。

As you can see variable _environment is not initialized with development value as it is promised here: https://webpack.js.org/configuration/mode/正如您所看到的,变量_environment没有像这里所承诺的那样使用development值初始化: https://webpack.js.org/configuration/mode/

======================================== =========================================

_environment is undefined because the environment variable NODE_ENV is undefined. _environment undefined ,因为环境变量NODE_ENV未定义。 You can solve this in one of three:您可以通过以下三种方式之一解决此问题:

  1. Invoking npm run watch --node-env=development : https://webpack.js.org/api/cli/#node-env调用npm run watch --node-env=development : https://webpack.js.org/api/cli/#node-env
  2. Exporting NODE_ENV in your current shell session:在您当前的 shell session 中导出NODE_ENV
     $ export NODE_ENV=production; npm run watch
  3. Updating your configuration to specify the value from some other source (eg an --env argument, a file on disk, hard-coding it, etc.)更新您的配置以指定来自其他来源的值(例如--env参数、磁盘上的文件、对其进行硬编码等)

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

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