React + webpack:'process.env'未定义

React + webpack:'process.env'未定义

问题描述:

我正在尝试使用webpack运行我们网站上的热门开发服务器;该网站使用ReactJS,其中包含以下代码:

I'm trying to run the hot dev server on our site with webpack; the site uses ReactJS, which has this code in it:

if (\"production\" !== process.env.NODE_ENV) // etc

当没有运行热插拔时,它很好,但是热 - 交换,它运行,导致错误:

When not running hot-swap it's fine, but with the hot-swap, it gets run, resulting in the error:

TypeError: process.env is undefined

代码如下所示:

项目建模在 https://github.com/webpack/react-starter 工作;所以问题是;我在配置文件中犯了什么错误和/或当'生产'编译工作正常时如何查找错误?

The project is modelled after https://github.com/webpack/react-starter which does work; so the question is; what error have I made in the config file and/or how do I go about looking for the error when the 'production' compilation works just fine?

我发布了 webpack配置文件的要点

在你的webpack配置中,有两个选项可以影响 process.env

In your webpack config, there are two options that can affect process.env:

  • When you specify config.target (see config.target)
  • When you define the process.env variable via DefinePlugin

查看代码,它看起来像 options.prerender 和 options.minimize $ c $时,c $ c> process.env 可能未定义c>是 false

Looking at your code, it looks like process.env might be undefined when both options.prerender and options.minimize are false.

您可以通过始终使用定义的环境来解决此问题process.env (例如: node ),或使用 DefinePlugin 分配默认值自己给变量赋值。

You could fix this by always using an environment that defines process.env (ex: node), or by using DefinePlugin to assign a default value to the variable yourself.