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

启用ESLint时遇Cannot read properties of undefined (reading 'getTokens')错误求助

解决ESLint报错:Cannot read properties of undefined (reading 'getTokens')

这个报错通常和ESLint相关依赖版本不兼容、TS配置问题或规则冲突有关,结合你的配置文件,可按以下步骤排查:

1. 统一@typescript-eslint相关依赖版本

@typescript-eslint/parser和@typescript-eslint/eslint-plugin必须保持完全一致的版本,否则极易出现内部API调用失败的问题。

  • 打开package.json,检查这两个包的版本是否相同
  • 若版本不一致,执行命令统一版本(以最新稳定版为例):
npm install @typescript-eslint/parser@latest @typescript-eslint/eslint-plugin@latest --save-dev

2. 检查ESLint核心版本兼容性

@typescript-eslint插件对ESLint核心版本有要求,比如v6+版本需要ESLint v8.23.0及以上。

  • 执行命令查看当前版本:
npm list eslint @typescript-eslint/parser
  • 若版本不匹配,升级ESLint:
npm install eslint@latest --save-dev

3. 验证tsconfig.json配置

你的parserOptions里指定了project: './tsconfig.json',需确保该文件配置正确:

  • 确认tsconfig.json路径无误(若ESLint配置在子目录,需调整路径)
  • 检查tsconfig的include字段是否覆盖了所有需要校验的文件,示例:
{
  "include": ["src/**/*", "pages/**/*"]
}
  • 可暂时注释project字段,若报错消失,说明是TS类型配置问题,需修复tsconfig的类型检查规则。

4. 清理冲突的扩展配置

你的extends数组存在冗余或顺序问题,调整如下:

  • 将plugin:prettier/recommended放在extends最后,确保它能覆盖其他格式规则
  • 移除重复扩展,比如eslint:recommended已被plugin:@typescript-eslint/recommended包含,可删除
  • 调整后的extends示例:
extends: [
  'next',
  'turbo',
  'airbnb',
  'plugin:react/recommended',
  'plugin:@typescript-eslint/eslint-recommended',
  'plugin:@typescript-eslint/recommended',
  'plugin:@typescript-eslint/recommended-requiring-type-checking',
  'plugin:prettier/recommended'
],

5. 清除ESLint缓存

缓存可能残留旧错误,执行命令清除:

npx eslint --cache .

或直接删除项目根目录的.eslintcache文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 19:33:25