S
S
Stanislav2018-02-04 10:38:24
linux
Stanislav, 2018-02-04 10:38:24

Does the NODE_ENV variable in production need to be set to 'production' only during the build of the project, or also during its lifetime?

There is a project, it is built on a production server (Debian) using the following script in package.json: 'cross-env NODE_ENV=production webpack'. Along with this, the following construction is used in the Webpack config:

new webpack.DefinePlugin({
    NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'production'),
}),

That is, in theory, we will definitely have the NODE_ENV variable set to 'production' at the time of assembly. Even if the cross-env utility doesn't work for us, we'll find in the Webpack config that process.env.NODE_ENV has no value and set NODE_ENV to 'production'. However, after we assemble the project in this way and look at it when opening it in the console in the browser, we will see something like this log:
Warning: It looks like you're using a minified copy of the development build of React. When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. See https://fb.me/react-minification for more details.
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 ( stackoverflow.com/questions/30030031) to ensure you have the correct code for your production build.

Naturally, I followed the links, and found nothing new there, the NODE_ENV variable at the time of assembly is almost certainly set as it should be. In this regard, the question is - perhaps it should be installed as such not only at the time of assembly? Or do some other things affect the appearance of these messages in the logs?
Specifications:
  1. Webpack version 2.x, minification occurs using the following construction:
    new UglifyJsPlugin({
      exclude: /node_modules/,
      sourceMap: true,
      parallel: true,
      uglifyOptions: {
        ie8: true,
        ecma: 6,
      },
    }),

    That is, not the plugin built into Webpack is used, but the one installed by a separate dependency "[email protected]", since with the built-in, version 0.4.x (sort of) had its own problems.
  2. I tried to build the project on my local environment with the same settings, I have Win 7, after which I uploaded it to the server - the behavior is similar.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-02-04
@lamo4ok

To do a production build, run webpack with the -p option :
or set the process.env.NODE_ENV variable and use the UglifyJsPlugin :

new webpack.DefinePlugin({
  'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}),
new webpack.optimize.UglifyJsPlugin({
       // options 
    }),

when launched with the -p key , these lines are added to the config.
Another option:
plugins: [
  new webpack.EnvironmentPlugin(['NODE_ENV']),
  new webpack.optimize.UglifyJsPlugin({
      // options 
    }),
],

Line: similar to:
new webpack.DefinePlugin({
  'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}),

And with NODE_ENV you will not make a production assembly, you must use process.env.NODE_ENV.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question