webpack options 有一个未知属性“hotOnly”。选项对象无效。开发服务器已使用选项对象初始化

Sib*_*enu 9 build ecmascript-6 reactjs webpack webpack-dev-server

npx webpack-dev-server --mode development我正在我的应用程序中运行该命令react并收到上述错误。

[webpack-cli] Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'hotOnly'. These properties are valid:
Run Code Online (Sandbox Code Playgroud)

下面是我的webpack.config.js文件。

const path = require("path");
const webpack = require("webpack");

module.exports = {
  entry: "./src/index.js",
  mode: "development",
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules)/,
        loader: "babel-loader",
        options: {
          presets: ["@babel/env"],
        },
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  resolve: {
    extensions: ["*", ".js", ".jsx"],
  },
  output: {
    path: path.resolve(__dirname, "dist/"),
    publicPath: "/dist/",
    filename: "bundle.js",
  },
  devServer: {
    contentBase: path.join(__dirname, "public/"),
    port: 3000,
    publicPath: "https://localhost:3000/dist/",
    hotOnly: true,
  },
  plugins: [new webpack.HotModuleReplacementPlugin()],
};
Run Code Online (Sandbox Code Playgroud)

知道是什么导致了这个问题吗?

Tus*_*try 20

因此 devServer|Webpack 配置与webpack-dev-server的选项相关 如果您的 webpack 使用 webpack-dev-server 版本 4 您应该使用此迁移指南

// your v3 config
devServer: {
    contentBase: path.join(__dirname, "public/"),
    port: 3000,
    publicPath: "https://localhost:3000/dist/",
    hotOnly: true,
  },
Run Code Online (Sandbox Code Playgroud)

在 v4 中将是

devServer: {
    // contentBase
    static : {
      directory : path.join(__dirname, "public/")
    },
    port: 3000,
    // publicPath
    devMiddleware:{
       publicPath: "https://localhost:3000/dist/",
    }
    // hotOnly
    hot: "only",
  },
Run Code Online (Sandbox Code Playgroud)


Sib*_*enu 10

看来更新版本webpack不支持该属性hotOnly,我们应该使用该选项hot您可以在此处查看与此相关的 GitHub 问题。

  devServer: {
    hot: "only", // hot:true
  },
Run Code Online (Sandbox Code Playgroud)

当您设置 hot: true 时,最新版本会自动应用 HotModuleReplacementPlugin 插件,因此如果您有 hot: true/hot: "only",请检查您的插件中是否没有 HotModuleReplacementPlugin。如果您有上述设置,您将收到一条警告,显示“ [webpack-dev-server] “hot: true”自动应用 HMR 插件,您无需手动将其添加到您的 webpack 配置中。 ”。

plugins: [new webpack.HotModuleReplacementPlugin()], 
Run Code Online (Sandbox Code Playgroud)

如果您收到错误“ static heartbeatInterval = 1000; SyntaxError: Unexpected token = ”,请确保按照此处的>= 12.13.0指南使用节点版本。

如果一切都完成,您应该能够在运行时看到如前所述的输出npx webpack-dev-server --mode development

在此输入图像描述

感谢 @Tushar Mistry 提供迁移指南。

下面是我完成的webpack.config.js文件。

const path = require("path");
const webpack = require("webpack");

module.exports = {
  entry: "./src/index.js",
  mode: "development",
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules)/,
        loader: "babel-loader",
        options: {
          presets: ["@babel/env"],
        },
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"],
      },
    ],
  },
  resolve: {
    extensions: ["*", ".js", ".jsx"],
  },
  output: {
    path: path.resolve(__dirname, "dist/"),
    publicPath: "/dist/",
    filename: "bundle.js",
  },
  devServer: {
    static: {
      directory: path.join(__dirname, "public/"),
    },
    port: 3000,
    devMiddleware: {
      publicPath: "https://localhost:3000/dist/",
    },
    hot: "only",
  },
};
Run Code Online (Sandbox Code Playgroud)

或者您也可以使用旧版本,如下所示。

"webpack": "4.41.5",
"webpack-cli": "3.3.10",
"webpack-dev-server": "3.10.1"
Run Code Online (Sandbox Code Playgroud)