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

CDK项目执行lint时出现allowShortCircuit未定义错误求助

问题描述
  • 执行 npm run lint 时触发错误:
    TypeError: Error while loading rule '@typescript-eslint/no-unused-expressions': Cannot read properties of undefined (reading 'allowShortCircuit')
    
  • 错误发生在检查 /path-to-project/cdk/bin/cdk.ts 文件时,调用栈片段:
    at Object.create (/path-to-project/node_modules/eslint/lib/rules/no-unused-expressions.js:75:13)
    at create (/path-to-project/node_modules/@typescript-eslint/eslint-plugin/dist/rules/no-unused-expressions.js:28:32)
    ...
    
  • 已尝试操作:
    • 删除 node_modules 并重新安装:rm -rf node_modules && npm install
    • 尝试确保 @typescript-eslint/eslint-plugin、eslint 等包版本兼容
  • 当前 ESlint 配置文件 eslint.config.mjs 内容:
    import eslint from '@eslint/js';
    import tseslint from 'typescript-eslint';
    
    export default tseslint.config(eslint.configs.recommended, ...tseslint.configs.recommended, {
        ignores: ['node_modules/', 'dist/', 'cdk.out/', 'build/'],
    });
    
问题分析与解决建议

核心原因

该错误本质是 @typescript-eslint/no-unused-expressions 规则初始化时,无法读取预设配置中的 allowShortCircuit 属性,大概率是以下两种情况:

  1. @typescript-eslint/eslint-plugin 与 eslint 版本不兼容,导致规则依赖的原生 ESlint 规则逻辑出现调用异常
  2. 配置中同时加载了原生 ESlint 的 no-unused-expressions 规则和 @typescript-eslint 的同名规则,引发初始化冲突

具体解决步骤

  1. 强制锁定兼容版本组合
    在 package.json 中明确指定匹配的版本(以当前稳定兼容组合为例):

    "devDependencies": {
      "eslint": "^8.57.0",
      "@typescript-eslint/eslint-plugin": "^7.1.0",
      "@typescript-eslint/parser": "^7.1.0"
    }
    

    然后执行彻底重装:

    rm -rf node_modules package-lock.json
    npm install
    
  2. 避免规则冲突,明确启用 TS 版规则
    修改 eslint.config.mjs,禁用原生 ESlint 的 no-unused-expressions 规则,只保留 @typescript-eslint 的版本:

    import eslint from '@eslint/js';
    import tseslint from 'typescript-eslint';
    
    export default tseslint.config(
      eslint.configs.recommended,
      ...tseslint.configs.recommended,
      {
        ignores: ['node_modules/', 'dist/', 'cdk.out/', 'build/'],
        rules: {
          'no-unused-expressions': 'off',
          '@typescript-eslint/no-unused-expressions': 'error'
        }
      }
    );
    
  3. 清理 ESlint 缓存
    缓存残留可能导致旧规则逻辑未更新,执行:

    npx eslint --clear-cache
    npm run lint
    
  4. 排查重复依赖
    检查项目中是否存在多版本的 ESlint 或 @typescript-eslint 包:

    npm ls eslint
    npm ls @typescript-eslint/eslint-plugin
    

    如果发现重复包,可通过 npm dedupe 合并,或手动删除冲突的依赖项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 14:16:13