【问题标题】:webpack options has an unknown property 'hotOnly'. Invalid options object. Dev Server has been initialized using an options objectwebpack 选项有一个未知属性“hotOnly”。无效的选项对象。开发服务器已使用选项对象初始化
【发布时间】:2021-11-05 04:18:23
【问题描述】:

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

[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:

下面是我的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()],
};

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

【问题讨论】:

    标签: reactjs webpack ecmascript-6 build webpack-dev-server


    【解决方案1】:

    webpack 的更新版本似乎不支持属性hotOnly,我们应该改用选项hot。您可以看到与此 here 相关的 GitHub 问题。

      devServer: {
        hot: "only", // hot:true
      },
    

    当你设置 hot: true 时,最新版本会自动应用 HotModuleReplacementPlugin 插件,所以如果你有 hot: true/hot: "only",请检查你的插件中没有 HotModuleReplacementPlugin。您将收到警告,因为“ [webpack-dev-server] "hot: true" 会自动应用 HMR 插件,您不必手动将其添加到您的 webpack 配置中。 em>" 如果您有上述设置。

    plugins: [new webpack.HotModuleReplacementPlugin()], 
    

    如果您收到错误“static heartbeatInterval = 1000; SyntaxError: Unexpected token =”,请确保按照指南 here 使用节点版本为 >= 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",
      },
    };
    

    或者你也可以使用下面的旧版本。

    "webpack": "4.41.5",
    "webpack-cli": "3.3.10",
    "webpack-dev-server": "3.10.1"
    

    【讨论】:

      【解决方案2】:

      所以 devServer|Webpack 配置与 webpack-dev-server 的选项有关 如果你的 webpack 使用的是 webpack-dev-server 版本 4,你应该使用这个 migration guide

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

      在 v4 中将是

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

      【讨论】:

      • 我得到了问题标题的错误,除了未知属性“contentBase”错误而不是“hotOnly”。使用您的“in v4 will be”建议为我解决了这个问题。谢谢图沙尔。
      • 另外,devMiddleware 属性后缺少逗号会导致错误。
      猜你喜欢
      • 2022-10-13
      • 1970-01-01
      • 2022-01-19
      • 2022-07-07
      • 2021-03-24
      • 2010-11-09
      • 2020-12-02
      • 2021-12-30
      • 1970-01-01
      相关资源
      最近更新 更多