您目前正在使用NODE_ENV ===' production'之外的缩小代码。这意味着您正在运行较慢的Redux开发版本

时间:2017-04-29 10:14:22

标签: reactjs webpack redux

我已经创建了一个基本应用并将其部署用于制作。运行webpack -p并启动服务器后,我现在收到此控制台日志错误:

You are currently using minified code outside of NODE_ENV === 'production'. This means that you are running a slower development build of Redux. You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify or DefinePlugin for webpack (http://stackoverflow.com/questions/30030031) to ensure you have the correct code for your production build.

这是我的webpack.config.js文件:

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};

特别是React和Webpack的新手。如果有人能够指出什么是错的并帮助我绕过webpack(这非常令人困惑)那么它将永远感激不尽!

修改

我添加了webpack插件并运行NODE_ENV=production webpack -p并在终端中收到此错误:

/Users/Joseph/workspace/weather-fcc/webpack.config.js:27
    new webpack.DefinePlugin({
        ^

ReferenceError: webpack is not defined
    at Object.<anonymous> (/Users/Joseph/workspace/weather-fcc/webpack.config.js:27:9)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at requireConfig (/usr/local/lib/node_modules/webpack/bin/convert-argv.js:96:18)
    at /usr/local/lib/node_modules/webpack/bin/convert-argv.js:109:17` FWIW my webpack version is 1.12.9

2 个答案:

答案 0 :(得分:15)

Webpack 4更新

如果您使用的是webpack&gt; = 4,则可以将mode设置为production。这将自动定义NODE_ENVproduction,而无需使用define插件。 https://webpack.js.org/concepts/mode/#mode-production

似乎webpack已更新了-p标记,以便在您的捆绑代码中自动定义process.env.NODE_ENVproduction(感谢@ x-yuri指出这一点)。因此,虽然下面的答案现在是不必要的,但我将其保留在那里作为参考,并作为webpack定义插件如何工作的解释。

原始答案+解释

TLDR :使用webpack define pluginprocess.env.NODE_ENV设置为生产。

长版:

React,Redux和许多其他JS库包括额外的验证和错误日志记录,使开发更容易。但是,您显然不希望在生产中使用它,因为这会使您的代码库更大更慢。这些检查通常包含在如下所示的语句中:

if (process.env.NODE_ENV !== 'production') {
  // do development checks
}

您遇到的错误是Redux告诉您虽然您已将代码缩小,但process.env.NODE_ENV未设置为production,因此开发检查仍然存在到位。要解决此问题,您需要使用webpack define pluginprocess.env.NODE_ENV设置为生产。

var webpack = require('webpack');

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  },
  plugins: [
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    }),
  ]
};

请注意,我将process.env.NODE_ENV的值定义为运行webpack时为您设置的值。因此,要生成生产捆绑包,您需要运行NODE_ENV=production webpack -p,而不仅仅是webpack -p。这将替换捆绑中process.env.NODE_ENVproduction的任何实例。所以我上面显示的检查现在看起来像这样:

if ('production' !== 'production') {
  // do development checks
}

Minifiers足够聪明,可以检测到if语句中的代码永远不会发生,并将从生产包中删除它。因此,您将获得两全其美:更好的开发体验,并且您的生产包中没有额外的代码:)

答案 1 :(得分:0)

在反应本机和redux中转到

node_modules / redux / src / index.js

删除或评论:

if (
  process.env.NODE_ENV !== 'production' &&
  typeof isCrushed.name === 'string' &&
  isCrushed.name !== 'isCrushed'
) {
  warning(
    'You are currently using minified code outside of NODE_ENV === "production". ' +
      'This means that you are running a slower development build of Redux. ' +
      'You can use loose-envify (https://github.com/zertosh/loose-envify) for browserify ' +
      'or setting mode to production in webpack (https://webpack.js.org/concepts/mode/) ' +
      'to ensure you have the correct code for your production build.'
  )
}