You need to enable JavaScript to run this app. 执行npx webpack-dev-server时出现webpack-cli模块缺失问题的解决方案
优惠活动
大模型
产品
解决方案
定价
更多

执行npx webpack-dev-server时出现webpack-cli模块缺失问题的解决方案

Webpack5 环境启动开发服务报MODULE_NOT_FOUND问题处理

现有项目配置

已安装的依赖配置

项目在package.json中已配置对应版本的依赖,核心devDependencies如下:

{
  "name": "test5",
  "version": "1.0.0",
  "description": "",
  "main": "webpack.config.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "css-loader": "^5.0.0",
    "file-loader": "^6.1.1",
    "html-webpack-plugin": "^4.5.0",
    "style-loader": "^2.0.0",
    "url-loader": "^4.1.1",
    "webpack": "^5.1.3",
    "webpack-cli": "^4.1.0",
    "webpack-dev-server": "^3.11.0"
  }
}

已编写的webpack配置

const {resolve} = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/index.js',
    output: {
        filename: 'built.js',
        path: resolve(__dirname, 'build')
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                exclude: /\.(js|html|css)$/,
                loader: 'file-loader',
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/webpack.html'
        })
    ],
    mode: 'development',

    //devServer配置
    devServer: {
        contentBase: resolve(__dirname, 'build'),
        compress: true,
        port: 3000,
        open: true
    }
};

触发的报错信息

在项目根目录执行npx webpack-dev-server尝试启动开发服务时,抛出MODULE_NOT_FOUND类型错误,完整报错如下:

Error: Cannot find module 'webpack-cli/bin/config/config-yargs'
Require stack:
- D:\webpack workspace\test5\node_modules\webpack-dev-server\bin\webpack-dev-server.js

at Object.<anonymous> (D:\webpack workspace\test5\node_modules\webpack-dev-server\bin\webpack-dev-server.js:65:1)
code: 'MODULE_NOT_FOUND',
requireStack: [
  'D:\\webpack workspace\\test5\\node_modules\\webpack-dev-server\\bin\\webpack-dev-server.js'
]

根因定位

通过排查webpack-dev-server的入口源码可以发现,v3.x版本的webpack-dev-server是硬编码兼容webpack-cli@3.x版本的内部模块路径的,对应的判断逻辑为:

// 优先匹配webpack-cli@3.3版本的模块路径
let configYargsPath;
try {
  require.resolve('webpack-cli/bin/config/config-yargs');
  configYargsPath = 'webpack-cli/bin/config/config-yargs';
} catch (e) {
  // 路径匹配失败时 fallback 到webpack-cli@3.x的另一路径
  configYargsPath = 'webpack-cli/bin/config-yargs';
}
// 直接引入匹配到的模块执行
require(configYargsPath)(yargs);

yargs.version(getVersions());
yargs.options(options);
const argv = yargs.argv;

项目当前安装的是webpack-cli@4.x版本,该版本对内部包结构做了重构,上述两个路径下的模块均已被移除,因此直接触发模块找不到的报错,不需要降级任何依赖版本即可解决问题。

已验证的可行解决方案

使用webpack serve命令替代原有的启动指令,即可正常调用webpack-dev-server拉起开发服务,两种操作方式任选其一即可:

  • 修改package.json的scripts配置,新增"dev": "webpack serve"启动项,后续执行npm run dev即可正常启动服务
  • 不修改配置文件,直接在项目根目录终端执行npx webpack serve,即可正常启动开发服务

内容的提问来源于stack exchange,提问作者Tokyo Mr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.20 21:24:10