[webpack4 learning record] Running npm run dev reports an error npm ERR! Failed at the [email protected] dev script.

Background:
The package.json file sets a quick run command, and passes the parameter 'development' to the env.mode field in the 'dev' field. The function is to set the webpack mode to development through the config configuration file.

  "scripts": {
    
    
    "webpack": "webpack",
    "dev": "npm run webpack -- --env.mode development"
  },

Receive the parameter env in the webpack.config.js file and print:

module.exports = (env)=> {
    
    
    console.log(env)
    return {
    
    
        mode:env.mode
    }
}

But this time it reported an error:
insert image description here



Reason:
After checking the answers from netizens, it turns out that the versions of webpack and webpack-cli are incompatible.
At that time, I installed it by default (webpack version is 5.4xx, webpack-cli is 4.7.2)

npm install --save-dev webpack

npm install --save-dev webpack-cli

Tried it, the latest version of webpack 5.49.0 and webpack-cli 4.7.2 are also incompatible

Can not be installed together

npm install webpack webpack-cli --save-dev


Solution:
Use the compatible version provided by netizens

npm install --save-dev webpack-cli@3.3.5

npm install --save-dev webpack@4.35.0   

The problem is solved, the printing is successful
insert image description here
If you want to install the html-webpack-plugin library, the adapted version is 4.4.1

npm install [email protected] --save-dev

If you want to install webpack-dev-server, the adapted version is 3.11.1

which is:

webpack:4.35.0
webpack-cli:3.3.5
webpack-dev-server:3.11.1
html-webpack-plugin:4.4.1

insert image description here

I also found two other combinations on the Internet, save them to prevent them from being found (unverified) when they are needed later, they are:

webpack:4.20.2
webpack-cli:3.1.2
webpack-dev-server:3.1.9

and

webpack -v —— "^5.1.3"
webpack-cli -v —— "^3.3.12"
webpack-dev-server -v —— "^3.11.0"

Guess you like

Origin blog.csdn.net/qq_43523725/article/details/119565351