You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级ESLint至9.0.0后React项目出现规则找不到错误

问题排查与解决方案

核心问题1:React插件配置错误

你的eslint.config.js中React插件的加载方式不符合ESLint Flat Config规范:

plugins: {
    react: { ...pluginReactAllConfig, ...pluginReactConfig },
    // ...其他插件
}

Flat Config的plugins字段需要直接引用插件的导出对象,而非插件的预设配置文件内容。你导入的pluginReactAllConfig和pluginReactConfig是预设规则集合,不是插件本身。

修复步骤:

  1. 修正导入语句,引入eslint-plugin-react插件本身:
// 替换原有的pluginReactAllConfig、pluginReactConfig导入
import pluginReact from "eslint-plugin-react";
import pluginReactAllConfig from "eslint-plugin-react/configs/all.js";
import pluginReactConfig from "eslint-plugin-react/configs/recommended.js";
  1. 在配置数组中单独添加React预设,同时修正plugins字段:
export default [
    pluginJs.configs.recommended,
    // 先加载React的预设配置
    pluginReactConfig,
    pluginReactAllConfig,
    {
        files: ["**/*.{js,jsx}"],
        ...jest.configs["flat/recommended"],
        plugins: {
            react: pluginReact, // 这里使用插件本身
            jest,
            import: importConfig,
            // ...其他插件保持不变
        },
        // ...其余配置内容
    }
];

核心问题2:使用了已废弃的React ESLint规则

错误提示中的react/jsx-closing-bracket-location规则,在你当前使用的eslint-plugin-react@7.34.1版本中已被移除。该规则的功能已合并到react/jsx-tag-spacing规则中。

修复步骤:

  • 移除rules中的"react/jsx-closing-bracket-location": "error"配置项
  • 若需要类似校验,可通过react/jsx-tag-spacing实现:
"react/jsx-tag-spacing": [
    "error",
    {
        "beforeSelfClosing": "always",
        "closingSlash": "never"
    }
]

额外注意事项

  • 确保所有依赖版本兼容:ESLint 9.x要求插件支持Flat Config,eslint-plugin-react@7.34.1已支持,但需避免混用旧版配置方式
  • 检查其他React规则是否存在废弃情况,比如react/jsx-closing-tag-location也可能在新版中被调整,可查阅插件版本对应的规则列表确认

内容的提问来源于stack exchange,提问作者Pradhumn Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 08:58:10