【已解决】运行webpack-dev-server时提示Error: Cannot find module ‘webpack-cli/bin/config-yargs‘
程序员文章站
2022-07-14 17:09:42
...
1.问题:
module.js:538
throw err;
^
Error: Cannot find module 'webpack/bin/config-yargs'
at Function.Module._resolveFilename (module.js:536:15)
at Function.Module._load (module.js:466:25)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object. (D:\saic\dev\expressVue\node_modules\[email protected]@webpack-dev-server\bin\webpack-dev-server.js:54:1)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
2.原因
当webpack与webpack-dev-server版本不匹配时,会出现如上错误。具体版本如下:
webpack-dev-server与webpack应该成对匹配使用,webpack4.x匹配webpack-dev-server3.x,webpack5.x匹配webpack-dev-server2.x,博主用的时候是最新版本5.x,无法与webpack-dev-server3.x兼容(图中已删除webpack-dev-server3.x)
3.解决方法
3.1 卸载本地旧包
cnpm uninstall webpack-dev-server --save-dev
3.2 重新下载一对匹配的依赖,这里举个跑通了的例子
cnpm install --save-dev [email protected]
cnpm install --save-dev [email protected]
重启webpack-dev-server
,成功跑通