构建TypeScript前端应用时@typescript-eslint/naming-convention规则未找到求助
解决“Definition for rule '@typescript-eslint/naming-convention was not found'”错误
试试下面几个排查和修复步骤:
1. 核对ESLint配置文件的插件与规则引入
打开你的ESLint配置文件(.eslintrc.js/.eslintrc.json等),确认:
- 已声明
@typescript-eslint为插件:"plugins": ["@typescript-eslint"] - extends数组中包含
plugin:@typescript-eslint/recommended(或其他包含naming-convention规则的@typescript-eslint配置集) - 如果手动配置了
@typescript-eslint/naming-convention规则,确保规则名拼写正确,且属于@typescript-eslint命名空间
示例配置片段:
module.exports = { parser: '@typescript-eslint/parser', plugins: ['@typescript-eslint'], extends: [ 'plugin:@typescript-eslint/recommended', 'eslint-config-react-app' ], rules: { '@typescript-eslint/naming-convention': [ 'error', { selector: 'variable', format: ['camelCase', 'UPPER_CASE'] } ] } };
注意:如果同时使用eslint-config-react-app,建议把@typescript-eslint的配置放在extends数组靠前位置,避免规则被覆盖
2. 清理依赖缓存并重装
依赖锁文件或node_modules可能存在版本冲突,执行以下操作:
- 删除
node_modules:rm -rf node_modules(Windows系统用rmdir /s/q node_modules) - 删除
package-lock.json:rm package-lock.json - 重新安装依赖:
npm install
3. 排查依赖版本兼容性
你的eslint-config-react-app@7.0.1可能与@typescript-eslint 5.x系列存在兼容性问题。执行npm ls @typescript-eslint/eslint-plugin查看依赖树,确认是否有嵌套安装的旧版本插件——这会导致ESLint加载错误的版本,找不到目标规则。
可以尝试:
- 升级
eslint-config-react-app到适配@typescript-eslint 5.x的版本 - 或者降级
@typescript-eslint/eslint-plugin和@typescript-eslint/parser到eslint-config-react-app依赖的版本
4. 检查Monorepo配置影响
你项目中使用了lerna和nx,这类monorepo工具可能会导致依赖加载路径异常。可以尝试在项目根目录执行npm run build时添加--no-hoist参数,或者检查nx/lerna的配置文件,确保@typescript-eslint相关依赖在子项目中正确被加载。
内容的提问来源于stack exchange,提问作者Newbie12920
相关产品推荐
相关产品推荐

