1 、使用webpack@4.32.2时, 当我通过package.json的script去执行webpack-dev-server时, 报以下错误:
Error: Cannot find module 'webpack/bin/config-yargs'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
......
原因: 这个就是目前版本的webpack-dev-server@2.11.5 不支持 webpack@4.32.2
解决: 重装一个webpack-dev-server是3.0版本以上就兼容 :
1
npm i webpack-dev-server@3.3.2 -D
此时, 就可以了... 如果还报错, 参照最后一条!
2、webpack4.0以上都要安装webpack-cli , 否则报错如下:
Error: Cannot find module 'webpack-cli/bin/config-yargs'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
at Function.Module._load (internal/modules/cjs/loader.js:507:25)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
这时, 需要安装webpack-cli :
npm i webpack-cli -D
3、 这样安装完后可能还会报错, 此时就需要卸载掉所有的 webpack webpack-cli webpack-dev-server
重新一起安装, 一定记住, 一起同时安装!!
npm i webpack webpack-cli webpack-dev-server -D
我的安装完后如下:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "webpack-dev-server"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jquery": "^3.5.1"
},
"devDependencies": {
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"
}
最终解决办法:把webpack 和webpack-cli以及webpack-dev-server版本号改成和我一样的,然后npm i下载 就可以了
"devDependencies": {
"webpack": "^4.44.2",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"