Why does production build of React app (with Webpack and Babel) use wrong development env with HMR, which causes errors? Why does production build of React app (with Webpack and Babel) use wrong development env with HMR, which causes errors? reactjs reactjs

Why does production build of React app (with Webpack and Babel) use wrong development env with HMR, which causes errors?


The only thing that worked for me, is that I wrote -

process.env.NODE_ENV = 'production';

at the beginning of my webpack.config.prod.js file.


It seems that no matter what Babel keeps using the development section of the env value specified in .babelrc. What solved the problem for me, was to use name other than 'development' and set that as the value of BABEL_ENV.

"env": {    "dev": {        "plugins": [        ]    },    "production": {    }}

I use separate conf for development. In plugins I have:

new webpack.DefinePlugin({  'process.env': {    'NODE_ENV': JSON.stringify('development'),    'BABEL_ENV': JSON.stringify('dev')  }}),


& in shell means that it will run in the background, so maybe your variable declaration is not caught by the build stuff that happens at the same time. The good thing is that you can just prepend the command with the variable declarations.

You could simplify the commands like this:

"serve": "NODE_ENV=development webpack-dev-server --content-base bin/ --devtool eval --progress --colors --hot --inline","deploy": "NODE_ENV=production BABEL_ENV=production webpack -p --config webpack.production.config.js"