bier hier bier hier - 24 days ago 6
Node.js Question

How can I set NODE_ENV?

I am using gulp in my reactjs solution and want to set the NODE_ENV, I tried this:

gulp.task('set-dev-node-env', function() {
return process.env.NODE_ENV = 'development';
});
//dev environment run
gulp.task('default', ['set-dev-node-env', 'webpack-dev-server']);


When I run gulp and check the chromeconsole it says:

testing process.env.NODE_ENV undefined


What am I missing or how can I set this NODE_ENV variable? I would like to set it in the gulpfile somehow.

Here is the complete project: github

Answer

Since you are using WebPack you can use the WebPack Define Plugin to inject the NODE_ENV

For example in your webpack.config.dev.js:

module.exports = {
  ...

  plugins: [
    ...
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify('development'),
    }),
  ],
};

Then in your react code you will have access to it NODE_ENV you set as process.env.NODE_ENV.