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

Yarn3+TypeScript Monorepo中如何统一管理ESLint插件依赖?

解决Yarn 3 Monorepo中ESLint共享配置依赖统一管理问题

问题原因

Yarn 3的PnP依赖隔离机制下,ESLint默认会从当前执行的业务包目录查找插件依赖。当你把ESLint插件移到共享配置包的devDependencies时,业务包的依赖树中没有这些插件,导致ESLint加载失败。

可行方案

方案1:配置ESLint插件解析路径(推荐)

这个方案无需修改依赖声明逻辑,仅通过ESLint配置指定插件查找位置:

  1. 调整共享配置包的依赖
    将@monorepo/eslint-config/package.json中原本在peerDependencies里的ESLint插件(如@typescript-eslint/eslint-plugin)移到dependencies,保留eslint作为peerDependency(业务包仍需安装ESLint核心):

    {
      "name": "@monorepo/eslint-config",
      "main": "index.js",
      "peerDependencies": {
        "eslint": "^8.27.0"
      },
      "dependencies": {
        "@typescript-eslint/eslint-plugin": "^5.42.1",
        "@typescript-eslint/parser": "^5.42.1",
        // 其他ESLint插件/配置
      }
    }
    
  2. 在共享配置中指定插件解析路径
    修改@monorepo/eslint-config/index.js,添加resolvePluginsRelativeTo选项,让ESLint从共享配置包的目录查找插件:

    const path = require('path');
    
    module.exports = {
      extends: [
        // 你的基础配置,如'eslint:recommended'等
      ],
      plugins: ['@typescript-eslint'],
      parser: '@typescript-eslint/parser',
      // 指定插件查找路径为当前共享配置包的根目录
      resolvePluginsRelativeTo: path.resolve(__dirname),
      // 其他规则配置...
    };
    
  3. 简化业务包依赖
    修改业务包package.json,仅保留必要依赖,移除所有ESLint插件:

    {
      "devDependencies": {
        "@monorepo/eslint-config": "workspace:*",
        "eslint": "^8.27.0"
      }
    }
    

方案2:使用Yarn packageExtensions(备选)

如果不想修改共享配置的依赖结构,可以通过Yarn的packageExtensions强制让业务包继承共享配置的插件依赖:

在根目录package.json中添加:

{
  "packageExtensions": {
    "@monorepo/eslint-config": {
      "dependencies": {
        "@typescript-eslint/eslint-plugin": "^5.42.1",
        "@typescript-eslint/parser": "^5.42.1",
        // 其他需要的插件
      }
    }
  }
}

执行yarn install后,业务包会自动安装这些依赖,但这种方式不够直观,不利于后续维护。

验证

执行根目录的lint命令:

yarn lint

此时ESLint应该能正常加载共享配置中的插件,无需业务包单独声明依赖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 16:50:43