【已解决】在 Vite 项目中使用 eslint-config-ali 时遇到的解析错误

错误还原

  1. 搭建 Vite 项目
pnpm create vite my-vue-app --template vue-ts
  1. 安装 eslint-config-ali
pnpm i -D eslint-config-ali @typescript-eslint/parser @typescript-eslint/eslint-plugin eslint-plugin-import eslint-import-resolver-typescript vue-eslint-parser eslint-plugin-vue
  1. 配置 .eslintrc
{
  "extends": ["eslint-config-ali/typescript/vue"]
}
  1. 安装并启用 VSCode 插件 ESLint

报错信息

查看 vite.config.ts 发现出错了,在第 1 行开始处的位置:

Parsing error: ESLint was configured to run on <tsconfigRootDir>/vite.config.ts using parserOptions.project: /tsconfig.json
However, that TSConfig does not include this file. Either:

  • Change ESLint’s list of included files to not include this file
  • Change that TSConfig to include this file
  • Create a new TSConfig that includes this file and include it in your parserOptions.project
    See the typescript-eslint docs for more info: https://typescript-eslint.io/linting/troubleshooting#i-get-errors-telling-me-eslint-was-configured-to-run–however-that-tsconfig-does-not–none-of-those-tsconfigs-include-this-file

解决办法

修改 .eslintrcparserOptions.project 配置,增加 tsconfig.node.json

{
  "extends": ["eslint-config-ali/typescript/vue"],
  "parserOptions": {
    "project": ["tsconfig.json", "tsconfig.node.json"]
  }
}