运行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": "^4.44.1",
"webpack-cli": "^3.3.12",
"webpack-dev-server": "^3.11.0"

其他回答

我通过在package.json上创建一个脚本命令来解决这个问题。

"dev": "webpack serve --config webpack.config.js --open",

尝试将webpack版本改为3.0,web-dev-server版本改为2.7.1

Eg:

"devDependencies": {
    "webpack": "^3.0.0",
    "webpack-cli": "2.0.13",
    "webpack-config-utils": "2.0.0",
    "webpack-dev-server": "^2.7.1",
    "webpack-validator": "2.2.7"
}

使用以下命令弃用webpack-cli版本:

npm install -D webpack-cli@3

新版本处于测试阶段,可能会修复这个错误。

将命令"serve":"webpack -dev-server"修改为"serve":"webpack serve"即可解决此问题。我已经在webpack开发服务器v3.11.0和v3.11.2上尝试了这个解决方案;对我来说,两者都很好。

这是因为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