执行npx webpack-dev-server,找不到模块

问题描述

package.json

  {
  "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.config.js

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
    }

};

问题:

  1. 当我执行npx webpack-dev-server

2.ErrorMessage:

    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.js

// webpack-cli@3.3 path : 'webpack-cli/bin/config/config-yargs'
let configYargsPath;
try {
  require.resolve('webpack-cli/bin/config/config-yargs');
  configYargsPath = 'webpack-cli/bin/config/config-yargs';
} catch (e) {
  configYargsPath = 'webpack-cli/bin/config-yargs';
}
// eslint-disable-next-line import/no-extraneous-dependencies
// eslint-disable-next-line import/no-dynamic-require
require(configYargsPath)(yargs);

// It is important that this is done after the webpack yargs config,// so it overrides webpack's version info.
yargs.version(getVersions());
yargs.options(options);

const argv = yargs.argv;

那么,该如何处理呢?我花了很多时间来做这个bug,但这不好。

(而且,package.json上的这些npm文件是最新的。)

请给我一些有效的提示。非常感谢

解决

        I have already solved this problem,we can use `webpack serve` to run `webpack-dev-server`
        so we just change the package.json file
    
        1."start-dev":"webpack-dev-server"
        to the
        "dev":"webpack serve"

     the final step is npm run dev;okay!successful!

2.First time we use npx webpack-dev-server
but Now we just write npx webpack serve in terminal
okay!successful

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)