Taylor Austin Taylor Austin - 1 month ago 10
Node.js Question

How to run webpack in production in a package.json script in windows with other commands?

I am using MERN stack and running windows 10. I am trying to run this

npm run
command from the
package.json
file.

"scripts": {
"build": "set NODE_ENV=production webpack && gulp",
"postinstall": "npm run build",
"start": "node ./bin/www"
},


When I run
npm run build
I get the following results:
terminal results

What happens is it looks like gulp runs and that is it. My bundle is not optimized for production at all. I will include the webpack file incase it is needed.

const webpack = require('webpack');
const path = require('path');

module.exports = {
entry: {
app: './src/app.js'
},
output: {
filename: 'public/build/bundle.js',
sourceMapFilename: 'public/build/bundle.map'
},
devtool: '#source-map',
plugins:
process.env.NODE_ENV === 'production'
? [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.UglifyJsPlugin({
minimize: true,
compress: {
warning: true
}
})
]
: [],
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-1']
}
}
]
}
};

Answer Source

You might want to use cross-env, a platform-independent way to use environment variables:

"scripts": {
    "build": "cross-env NODE_ENV=production webpack && gulp",
    // or if the former does not work
    "build": "npm run build:webpack && gulp",
    "build:webpack": "cross-env NODE_ENV=production webpack",
},

Note that there are other ways to do production builds or evaluate environment variables in the webpack configuration.