运行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的全球安装。公平地说,npm在安装webpack-dev-server时确实抱怨过:

未满足对等依赖webpack@^2.2.0

其他回答

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

打开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, {

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

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

请使用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-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