如何修复 eslintrc 文件与您的项目配置不匹配?

ome*_*ega 28 node.js typescript eslint

在我的打字稿项目中,我使用的是 eslint。下面的文件在根目录中,我还有子文件夹/dist/src.

eslintrc.js

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: {
    tsconfigRootDir: __dirname,
    project: ['./tsconfig.json'],
  },
  rules: {
    strict: 'error',
    semi: ['error', 'always'],
    'no-cond-assign': ['error', 'always'],
  },
  plugins: ['@typescript-eslint'],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking',
  ],
}
Run Code Online (Sandbox Code Playgroud)

配置文件

{
  "compilerOptions": {
    "outDir": "dist",
    "noImplicitAny": true,
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "module": "ES2020",
    "target": "ES2020",
    "lib": ["ES2020"],
    "allowJs": false,
    "alwaysStrict": true
  },
  "include": ["src"]
}
Run Code Online (Sandbox Code Playgroud)

module顶部的单词有一条带有此错误的红线

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: .eslintrc.js.
The file must be included in at least one of the projects provided. eslint
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

Aid*_*din 90

这个错误是关于什么的?

该错误基本上是说文件eslintrc.js 本身是:

  1. 由 ESLint 发现,但是
  2. 与它应该检查的文件列表不匹配

所以,ESLint 不知道该怎么办并且吓坏了!

请注意,虽然此文件不包含您的代码,但您应该决定是否要对该文件进行 linted(例如制表符与空格,或双引号与单引号)。


应该做什么?

您可以从上面的冲突情况中省略 (1) 或 (2)。

解决方案 1:告诉 ESLint 忽略该文件

更容易了。只需将文件名(例如.eslintrc.js)添加到.eslintignore.

您还可以查看我的答案的案例 1.3,了解不同的实现方式。

  • 优点:快。简单的。无痛。
  • 缺点:此文件的格式在制表符/空格或引号样式方面可能与其他文件不一致。

解决方案 2:告诉 ESLint 对该文件进行 lint 检查(首选)

tsconfig.jsonTypescript-ESLint 从via获取要包含的文件列表parserOptions > project

因此,您可以将该文件添加到现有文件tsconfig.json(解决方案 2.1)中,也可以创建一个辅助文件tsconfig.eslint.json来包含这样的文件,您希望通过 typescript-eslint 对该文件进行 linting,但不一定由 Typescript 编译器进行处理。

  • 优点:该文件和其他类似的 .js 配置也将包含在内,以保持一切一致。
  • 缺点:您可能不希望这些配置文件被检查。而且,无论如何,这都需要一些额外的工作。

解决方案2.1:将其添加到现有的tsconfig.json中

// in tsconfig.json ...

    "include": [
        ".eslintrc.js",
        // ...
    ]
Run Code Online (Sandbox Code Playgroud)

解决方案 2.2:将其添加到单独的tsconfig.eslint.json文件中 创建一个包含以下内容的文件名tsconfig.eslint.json

// new file: tsconfig.eslint.json

{
    "include": [
        ".eslintrc.js"
    ]
}
Run Code Online (Sandbox Code Playgroud)

然后将其注入到eslintrc.js's parserOptions> project(是的,project接受字符串和字符串数组):

// in eslintrc.js ...

  parserOptions: {
    project: [
        resolve(__dirname, './tsconfig.json'),
        resolve(__dirname, './tsconfig.eslint.json'),
    ],
  }
Run Code Online (Sandbox Code Playgroud)

附言。这个答案非常相似,我应该给予一些信任。

  • 谢谢,将 `.eslintrc.js` 添加到项目中明确地解决了我的问题。我认为我的全局模式应该覆盖它,但显然它们没有。 (2认同)
  • 这应该是现在的首选答案。不过,现在我想知道我是否应该忽略“文件是一个 CommonJS 模块;它可能会转换为 ES module.ts(80001)”,或者是否有办法指定这些配置文件应该是 CommonJS 模块 (2认同)

Joe*_*son 54

更新您的内容eslintrc.js以包括:

忽略模式:['.eslintrc.js']

  • 最好将其添加到您的 tsconfig.json 中:/sf/answers/4499819761/ (3认同)

ber*_*nie 9

现在,这已记录在 TypeScript ESLint 的文档中,我收到错误消息,告诉我“该文件必须包含在至少一个提供的项目中”

TLDR:请参阅下面的“附加选项”,了解我认为的最佳解决方案。

这是带有添加注释的相关文档。这是建立在之前的答案的基础上的。

此错误意味着正在检查的文件未包含在您提供给我们的任何 tsconfig 文件中。很多时候,当用户拥有未包含在正常 tsconfig 中的测试文件或类似文件时,就会发生这种情况。

取决于您想要实现的目标:

  • 如果您不想检查文件:
  • 如果您确实想检查文件:
    • 如果您不想使用类型感知 linting 对文件进行 lint:
      • 使用ESLint的overrides配置将文件配置为不使用类型信息进行解析。
        • 一种流行的设置是从顶级配置中省略上述添加内容,并仅通过覆盖将它们应用到 TypeScript 文件。=> 请参阅下面的附加选项
        • 或者,您可以parserOptions: { project: null }为要排除的文件添加覆盖。请注意,这{ project: undefined }将不起作用。=> 请参阅下面的减法选项
    • 如果您确实想使用类型感知 linting 对文件进行 lint:
      • 检查您提供的每个 tsconfig 的包含选项parserOptions.project- 您必须确保所有文件都与includeglob 匹配,否则我们的工具将无法找到它。 =>参见这个答案的解决方案2.1
      • 如果您的文件不应该是现有 tsconfig 之一的一部分(例如,它是存储库本地的脚本/工具),请考虑tsconfig.eslint.json在项目根目录中创建一个新的 tsconfig (我们建议调用它),其中列出了此文件文件包含在其中。有关示例,您可以查看我们在此存储库中使用的配置: => 请参阅此答案的解决方案 2.2 或下面的示例

添加剂选项

使用此解决方案,您只需添加(即启用)parser: '@typescript-eslint/parser'TypeScript 源文件的选项(以及其他相关选项)。其他文件(如)eslintrc.js不会受到该配置的影响,但仍会根据配置的其余部分进行检查。

将其添加到eslintrc.js

overrides: [
  {
    files: ['src/**/*.ts'],
    parser: '@typescript-eslint/parser',
    parserOptions: {
      tsconfigRootDir: __dirname,
      project: ['./tsconfig.json'],
    },
    rules: {},
  },
],
Run Code Online (Sandbox Code Playgroud)

上面的配置仅对src目录中的文件使用 TypeScript ESLint。

请注意,所有 TypeScript ESLint 规则也需要位于条目中overrides(即不是根rules选项),否则您将收到如下错误消息:

Error: Error while loading rule '@typescript-eslint/dot-notation': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.
Run Code Online (Sandbox Code Playgroud)

减法选项

使用此解决方案,您可以删除(或) 配置文件parser: '@typescript-eslint/parser'的选项。eslintrc.jseslint.cjs

将其添加到eslintrc.js

overrides: [
  {
    files: ['./.eslintrc.js'],
    parserOptions: { project: null },
    rules: {},
  },
],
Run Code Online (Sandbox Code Playgroud)

上述配置禁用该eslintrc.js文件的 TypeScript ESLint。您还可以添加其他文件或文件夹来override.files扩展此行为。

缺点:所有 TypeScript ESLint 规则也需要在条目中删除overrides,否则您将收到如下错误消息:

Error: Error while loading rule '@typescript-eslint/dot-notation': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.
Run Code Online (Sandbox Code Playgroud)


小智 -2

同样的错误,这对我有用: https: //github.com/nestjs/nest/issues/4900#issuecomment-669743374 https://github.com/nestjs/nest/issues/4900#issuecomment-707330674

看起来配置文件必须命名为 .eslintrc 并使用 json 格式而不是 .eslintrc.js 并且 parserOptions.createDefaultProgram 必须设置为 true