运行webpack-dev-server——config config/webpack.dev.js——progress——profile——watch——content-base src/时出错。下面是错误日志:

module.js:442
throw err;
^

Error: Cannot find module 'webpack/bin/config-yargs'
at Function.Module._resolveFilename (module.js:440:15)
at Function.Module._load (module.js:388:25)
at Module.require (module.js:468:17)
at require (internal/module.js:20:19)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3) 

当前回答

这是因为webpack-cli版本的变化。

如果webpack-cli版本号小于4。使用webpack-dev-server 如果webpack-cli版本号为4。X或更高,你可以使用webpack服务

3.执行命令webpack-cli。X及以下

    "scripts": {
      "dev-server": "webpack-dev-server"
    }

对于webpack-cli。X及以上

    "scripts": {
      "dev-server": "webpack serve"
    }
    "scripts": {
      "dev-server": "webpack serve "
    }

来源:webpack dev-server

其他回答

更新Webpack版本(和Webpack命令行):

npm install --save-dev webpack webpack-cli webpack-dev-server webpack-merge

如果您不使用上面提到的任何一种,请随意省略。

所以,首先你要排除node_modules文件夹。 - verificad后的>如果在归档包中。Json的依赖:"webpack", "webpack-cli"和"webpack-dev-server",他们在"dependencies":{}。 ->结束,打开终端执行:yarn命令。安装全部取决于再次排除。

解决方案

package.json

"scripts": {
    "startdev": "webpack serve --mode development --env development --hot --port 3000"
    ...
    ...
  },
"devDependencies": {
...
    "webpack": "^5.10.1",
    "webpack-cli": "^4.2.0"
 },

控制台

$ npm run startdev

要升级所有的包(在安装webpack-cli和webpack-dev-server之后),可以

npm --depth=9999 upgrade

这应该可以修复版本不匹配的问题。

下调webpack-cli

npm install -D webpack-cli@3