提问者:小点点

您当前使用的是节点_ENV=='production'之外的小型代码。这意味着您正在运行较慢的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.

这是我的网页。配置。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: './'
  }
};

我特别是对反应和网页包很新。如果有人能指出哪里出了问题,帮我把脑袋绕到网页包上(这很让人困惑),那我将永远感激你!

编辑:

我添加了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个答案

匿名用户

如果您正在使用webpack

webpack似乎还更新了-p标志,以自动定义流程。环境。将节点_ENV添加到捆绑代码中的生产(感谢@x-yuri指出这一点)。因此,虽然下面的答案现在是不必要的,但我仍将其保留在那里以供参考,并解释webpack define插件是如何工作的。

TLDR:使用webpack定义插件设置进程。环境。节点_ENV到生产。

长版本:

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

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

您得到的错误是Redux告诉您,尽管您已经缩小了代码,但仍需要进程。环境。节点_ENV未设置为生产,因此开发检查仍在进行中。要解决这个问题,您需要使用webpack define插件来设置进程。环境。节点_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=生产webpack-p,而不仅仅是webpack-p。这将用包中的生产替换process.env.NODE_ENV的任何实例。所以我上面显示的支票现在看起来像这样:

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

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

匿名用户

在react native和redux中,转到

节点\模块/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.'
  )
}