React + webpack:'process.env'未定义 [英] React + webpack: 'process.env' is undefined

查看:186
本文介绍了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

查看代码,它看起来像<$当$ code> options.prerender 和 options.minimize 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.

这篇关于React + webpack:'process.env'未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆