运行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 <= 3。X webpack-dev-server包工作正常。对于webpack-cli >= 4。X,使用NPX webpack serve命令运行本地服务器。

For webpack-cli 3.x:
"scripts": {
  "start:dev": "webpack-dev-server --mode=development"
}

For webpack-cli 4.x:
"scripts": {
  "start:dev": "webpack serve --mode=development"
}

其他回答

问题出在webpack-cli的新版本上。如果webpack-cli <= 3。X webpack-dev-server包工作正常。对于webpack-cli >= 4。X,使用NPX webpack serve命令运行本地服务器。

For webpack-cli 3.x:
"scripts": {
  "start:dev": "webpack-dev-server --mode=development"
}

For webpack-cli 4.x:
"scripts": {
  "start:dev": "webpack serve --mode=development"
}

尝试将webpack版本从1更改。X到2。X在你的package.json:

Eg:

 "devDependencies": {
    "webpack": "2.2.0-rc.3",
    "webpack-dev-server": "2.1.0-beta.0",
    "webpack-validator": "^2.3.0"
  }

当你使用webpack-dev-server的预发布版本和webpack的发布版本时,有时会发生这种情况,反之亦然。

下调webpack-cli

npm install -D webpack-cli@3

我刚刚解出来了。

问题出在index.html的路径上。

来自:

<script src="./dist/myBundle.js"></script>

to:

<script src="myBundle.js"></script>

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

npm --depth=9999 upgrade

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