Webpack-dev-server 抛出有关 node_modules/webpack-dev-server 中丢失文件的错误

Webpack-dev-server throws errors about files missing in node_modules/webpack-dev-server

我试图简单地 运行 webpack serve -c myconfig.js,但我不断收到这些错误:

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 6:16-37
Module not found: Error: Can't resolve 'strip-ansi' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 8:13-32
Module not found: Error: Can't resolve './socket' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 10:14-34
Module not found: Error: Can't resolve './overlay' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 12:15-37
Module not found: Error: Can't resolve './utils/log' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 16:18-48
Module not found: Error: Can't resolve './utils/sendMessage' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 18:16-44
Module not found: Error: Can't resolve './utils/reloadApp' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

ERROR in ./node_modules/webpack-dev-server/client/index.js?http://localhost:9000 20:22-56
Module not found: Error: Can't resolve './utils/createSocketUrl' in '/home/aironside/Documents/private/webpack-example/node_modules/webpack-dev-server/client'

如果我尝试添加热重载,情况会变得更糟(额外的错误,都是关于 node_modules 中缺失的内容)。

代码如下:

package.json

{
  "name": "webpack-example",
  "version": "1.0.0",
  "description": "",
  "private": "true",
  "scripts": {
    "dev": "npx webpack serve -c webpack.dev.js",
    "build": "npx webpack --config webpack.prod.js"
  },
  "repository": {
    "type": "git",
    "url": "git+ssh://git@gitlab.com/iron.alex/webpack-example.git"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "clean-webpack-plugin": "^4.0.0-alpha.0",
    "css-loader": "^5.2.4",
    "ftp-deploy": "^2.4.1",
    "html-loader": "^2.1.2",
    "html-webpack-plugin": "^5.3.1",
    "mini-css-extract-plugin": "^1.5.0",
    "node-sass": "^5.0.0",
    "sass": "^1.32.11",
    "sass-loader": "^11.0.1",
    "style-loader": "^2.0.0",
    "ts-loader": "^9.1.0",
    "typescript": "^4.2.4",
    "webpack": "^5.35.1",
    "webpack-cli": "^4.6.0",
    "webpack-dev-server": "^3.11.2",
    "webpack-merge": "^5.7.3"
  }
}

webpack.dev.js

const path = require("path");
const common = require("./webpack.common.js");
const { merge } = require("webpack-merge");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = merge(common, {
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "dev"),
  },
  module: {
    rules: [
      {
        test: /\.scss$/i,
        use: ["style-loader", "css-loader", "sass-loader"],
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html",
    }),
  ],
  mode: "development",
  //   https://www.youtube.com/watch?v=yR25JoybTxo
  devServer: {
    contentBase: path.join(__dirname, "dev"),
    compress: true,
    port: 9000,
  },
});

webpack.common.js

const path = require("path");

module.exports = {
  entry: {
    index: "./src/scripts/index.ts",
    library: "./src/scripts/library.ts",
  },
  resolve: {
    extensions: [".ts"],
  },
  module: {
    rules: [
      {
        test: /\.ts?$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
      {
        test: /\.html$/i,
        loader: "html-loader",
      },
    ],
  },
};

我尝试重新安装 node_modules,但没有成功。我在 github 上添加了一个问题,但不确定 do/how 还有什么可以修复它。

https://github.com/webpack/webpack-dev-server/issues/3221

知道我遗漏了什么吗?

没有 serve

的 Webpack 构建

问题出在

resolve: {
  extensions: [".ts"],
},

应该是['.ts','.js',/* 其他扩展名*/]。

有关详细信息,请参阅 here