运行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服务运行webpack-dev-server

webpack serve --config config/webpack.dev.js --progress --profile --watch --content-base src/

其他回答

一般情况是由于Webpack与Webpack -dev-server版本不兼容。 就像我也有这个问题,我的电脑的webpack是1.15.0,但webpack-dev-server是2。X以上版本。 所以我卸载webpack-dev-server: npm卸载webpack-dev-server -g 然后安装1.15.0版本的webpack-dev-server,你可以通过npm install webpack-dev-server@1.15.0 -g来解决这个问题

尝试将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": "^4.5.0",
 "webpack-dev-server": "^3.11.2"

此错误出现在yarn启动过程中,包中的以下条目。特定于脚本的Json。attr开始。

 "scripts": {
    "start": "webpack-dev-server --open",
    "build": "webpack"
  }

所以,webpack-dev-server——open正在寻找webpack-cli 3.3版本。我通过安装特定版本的webpack-cli工作包摆脱了这个错误。Json是这样的:

  "webpack-cli": "3.3",
  "webpack-dev-server": "^3.11.2"

但是,如果你不想降级webpack-cli版本,将"start": "webpack-dev-server——open"更新为"start": "webpack serve——open"

上面的答案对我都没用。如果你仍然得到这个错误,你可以试试这个,这解决了我的问题:

打开node_modules \ webpack-dev-server \ bin \ webpack-dev-server.js 修改第84行:require('webpack-cli/bin/config-yargs')(yargs);

To:

要求(webpack-cli / bin / config / config-yargs) (yargs); 修改第92行:const config = Require ('webpack-cli/bin/convert-argv')(yargs, argv, {

To:

const config = require('webpack-cli/bin/utils/convert-argv')(yargs, argv, {

这是webpack版本问题 你需要使用这个命令更新你的webpack

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

现在包装好了。Json文件的使用

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