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

如何在Turborepo中正确配置ESLint?配置异常求助

Turborepo中ESLint自定义配置未加载的排查方案

常见问题及解决办法

1. 自定义配置包的package.json入口配置错误

ESLint默认读取配置包的main字段指定的文件,若用TypeScript编写配置,需注意:

  • 若直接使用TS文件作为入口,需在package.json中配置type: "module",同时确保已安装@typescript-eslint/parser让ESLint能解析TS配置
  • 示例正确配置:
{
  "name": "eslint-config-custom",
  "main": "index.ts",
  "type": "module",
  "dependencies": {
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "eslint": "^8.0.0"
  }
}

如果是先编译TS为JS,需将main指向编译后的dist/index.js。

2. 应用中ESLint扩展路径不正确

在应用的.eslintrc.cjs或.eslintrc.json里,扩展自定义配置直接用包名即可,不要写相对路径:

{
  "extends": ["custom"]
}

写相对路径(比如../packages/eslint-config-custom)会导致Turborepo的workspace链接逻辑失效,生成不完整的依赖包。

3. 本地包未正确链接

别在单个应用目录单独执行npm install,必须在Turborepo根目录执行npm install,让workspace机制正确链接本地包。单独在应用目录安装会导致node_modules/eslint-config-custom只包含依赖,缺失你的自定义规则代码。

4. 自定义配置的导出格式错误

ESLint配置必须导出合法的配置对象,注意TS和JS的导出差异:

  • TS文件(index.ts)需用ES模块导出:
export default {
  env: { browser: true, es2020: true },
  extends: [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  parser: "@typescript-eslint/parser",
  plugins: ["@typescript-eslint"],
  rules: {
    "@typescript-eslint/no-unused-vars": "error",
    "no-console": "warn"
  }
}
  • CommonJS文件需用module.exports导出:
module.exports = {
  // 配置内容
}

5. ESLint版本冲突

确保自定义配置包和应用中的ESLint版本一致,版本不兼容会导致规则无法加载。可以在根目录package.json中统一指定版本:

{
  "devDependencies": {
    "eslint": "^8.50.0"
  }
}

验证步骤

  1. 在Turborepo根目录重新执行npm install,确保所有workspace包正确链接
  2. 在应用目录运行npx eslint .,检查自定义规则是否生效
  3. 查看node_modules/eslint-config-custom下是否存在你的index.ts(或编译后的JS文件),不存在则说明链接失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 13:21:22