You need to enable JavaScript to run this app. TypeScript React集成react-refresh-webpack-plugin报SocketClient非构造函数错误
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript React集成react-refresh-webpack-plugin报SocketClient非构造函数错误

错误原因

SocketClient is not a constructor 报错核心是 @pmmmwh/react-refresh-webpack-plugin 与 webpack-dev-server 版本不兼容:
webpack-dev-server v4 及以上版本重构了 Socket 客户端的导出逻辑,低于 0.5.0 版本的 @pmmmwh/react-refresh-webpack-plugin 未适配该改动,调用旧版导出结构时就会抛出该错误。

解决方案

按以下步骤调整即可修复:

  • 第一步:升级适配版本
    先卸载现有不兼容依赖:
npm uninstall @pmmmwh/react-refresh-webpack-plugin react-refresh

再安装适配 webpack-dev-server v4+ 的版本:

npm install @pmmmwh/react-refresh-webpack-plugin@latest react-refresh@latest -D
  • 第二步:修正 webpack 插件数组逻辑
    当前配置的 plugins 数组在生产环境会存在 false 假值,webpack 不允许插件数组内存在非插件实例的无效值,需要添加过滤逻辑:
plugins: [
  new HtmlWebpackPlugin({
    template: './client/index.html',
    filename: './index.html',
  }),
  isDevelopment && new HotModuleReplacementPlugin(),
  isDevelopment && new ReactRefreshWebpackPlugin(),
].filter(Boolean), // 新增该行过滤无效假值
  • 第三步:可选优化(React 17+ 适用)
    将 tsconfig.json 中 compilerOptions.jsx 配置从 react 改为 react-jsx,不需要手动导入 React 即可使用 JSX 语法,也能提升热更新识别效率。
  • 第四步:清除缓存重装依赖
    如果调整后仍报错,删除项目的 node_modules 文件夹、package-lock.json 或 yarn.lock 文件后重新执行安装命令即可。

内容的提问来源于stack exchange,提问作者Omer Dital

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 07:57:04