'How do I set webpack mode

I'm trying to configure my script (in package.json) in such a way that I can run webpack in production or development mode via cli.

package.json

"scripts": {
"start": "webpack serve",
"build": "webpack --mode=production"

in my webpack.config.js file, I was expecting process.env.NODE_ENV to equal to whatever I set the mode to be. Instead, I keep getting undefined please how can I make it work



Solution 1:[1]

For the Dev Server

First, install webpack-dev-server with following command:

npm install webpack-dev-server

Add following line to the package.json script:

"scripts": {
    "start": "webpack-dev-server --mode development"
}

For Production build Add the following line to the scripts section of package.json:

"build": "node_modules/.bin/webpack --mode production"

The scripts section in package.json with both the dev and production build will look like this:

"scripts": {
    "build": "node_modules/.bin/webpack --mode production",
    "start": "webpack-dev-server --mode development" 
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sharath