简体   繁体   English

“parserOptions.project” 已为 @typescript-eslint/parser 设置

[英]"parserOptions.project" has been set for @typescript-eslint/parser

I created a new React Native project with --template typescript我使用--template typescript创建了一个新的 React Native 项目

I deleted the template directory which came as part of the boilerplate.我删除了作为样板一部分的template目录。

I then proceeded to add ESLint:然后我继续添加 ESLint:

module.exports = {
  parser: "@typescript-eslint/parser",
  plugins: ["@typescript-eslint"],
  extends: ["airbnb-typescript-prettier"]
};

However, when I open babel.config.js , I get this error但是,当我打开babel.config.js时,出现此错误

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.解析错误:已为 @typescript-eslint/parser 设置“parserOptions.project”。

The file does not match your project config: /Users/Dan/site/babel.config.js .该文件与您的项目配置不匹配: /Users/Dan/site/babel.config.js

The file must be included in at least one of the projects provided.eslint该文件必须至少包含在提供的项目之一中。eslint

Different lint rules for JavaScript and TypeScript files JavaScript 和 TypeScript 文件的不同 lint 规则

The problem happens for one of the reasons below:出现此问题的原因之一如下:

  1. You're using a rule which require type information and didn't specify a parserOptions.project ;您正在使用需要类型信息且未指定parserOptions.project的规则;
  2. You specified parserOptions.project , didn't specify createDefaultProgram ( it will be removed in a future version ), and you're linting files not included in the project (eg babel.config.js , metro.config.js )您指定parserOptions.project ,没有指定createDefaultProgram它将在未来的版本中删除),并且您正在检查项目中未包含的文件(例如babel.config.jsmetro.config.js

As from the TypeScript ESLint Parser docs :TypeScript ESLint Parser 文档开始

parserOptions.project parserOptions.project

This option allows you to provide a path to your project's tsconfig.json .此选项允许您提供项目的tsconfig.json的路径。 This setting is required if you want to use rules which require type information.如果要使用需要类型信息的规则,则需要此设置。

(...) (...)

Note that if this setting is specified and createDefaultProgram is not, you must only lint files that are included in the projects as defined by the provided tsconfig.json files.请注意,如果指定了此设置并且未指定createDefaultProgram ,则您必须仅对项目中包含的文件进行 lint,这些文件由提供的tsconfig.json文件定义。 If your existing configuration does not include all of the files you would like to lint, you can create a separate tsconfig.eslint.json .如果您现有的配置不包括您想要 lint 的所有文件,您可以创建一个单独的tsconfig.eslint.json

To solve it, update your ESLint config to use TypeScript rules only on TypeScript files:要解决它,请更新您的 ESLint 配置以仅在 TypeScript 文件上使用 TypeScript 规则:

{
  // ...
  parser: '@typescript-eslint/parser',
  plugins: ["@typescript-eslint"],
  overrides: [
    {
      files: ['*.ts', '*.tsx'], // Your TypeScript files extension

      // As mentioned in the comments, you should extend TypeScript plugins here,
      // instead of extending them outside the `overrides`.
      // If you don't want to extend any rules, you don't need an `extends` attribute.
      extends: [
        'plugin:@typescript-eslint/recommended',
        'plugin:@typescript-eslint/recommended-requiring-type-checking',
      ],

      parserOptions: {
        project: ['./tsconfig.json'], // Specify it only for TypeScript files
      },
    },
  ],
  // ...
}

You can read more about the overrides config on the official docs: How do overrides work?您可以在官方文档中阅读有关overrides配置的更多信息: 覆盖如何工作?


Don't lint a specific file不要对特定文件进行 lint

If you don't want to lint the file that is mentioned in the error (eg babel.config.js ), you can ignore it adding its name to the .eslintignore file :如果您不想 lint 错误中提到的文件(例如babel.config.js ),您可以忽略它,将其名称添加到.eslintignore文件中

babel.config.js

Anyway, the step above (about overriding the config for TypeScript files) is important in case your project contains both JavaScript and TypeScript files that you want to lint.无论如何,如果您的项目同时包含 JavaScript 和 TypeScript 文件,上述步骤(关于覆盖 TypeScript 文件的配置)很重要。

You can also create other overrides for different situations, eg a different config for test files, since it can use developer dependencies and run on node environment , instead of browser .您还可以为不同的情况创建其他覆盖,例如测试文件的不同配置,因为它可以使用开发人员依赖项并在node 环境而不是browser上运行。

You can create a separate TypeScript config file ( tsconfig.eslint.json ) intended for eslint configuration.您可以为 eslint 配置创建一个单独的eslint配置文件 ( tsconfig.eslint.json )。 That file extends tsconfig configuration and setups include key for files that have to be linted.该文件扩展tsconfig配置,并且设置include必须检查的文件的密钥。

.eslint.js : .eslint.js

// ...
parserOptions: {
  // ...
  project: "./tsconfig.eslint.json",
  // ...
},
// ...

tsconfig.eslint.json : tsconfig.eslint.json

{
  "extends": "./tsconfig.json",
  "include": [
    // ...
    "babel.config.js"
  ]
}

Or if you want to ignore it, you can put it into .eslintignore .或者如果你想忽略它,你可以把它放到.eslintignore中。

.eslintignore : .eslintignore

// ...
babel.config.js

Add one line in ".eslintignore":在“.eslintignore”中添加一行:

.eslintrc.js

You need to add that file to your tsconfig include array.您需要将该文件添加到您的 tsconfig include数组中。 See typescript-eslint/typescript-eslint#967 for more details.有关更多详细信息,请参阅typescript-eslint/typescript-eslint#967

In my ESLint config I have the following configuration:在我的 ESLint 配置中,我有以下配置:

.eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true,
  },
  globals: {
    Promise: "readonly"
  },
  parser: "@typescript-eslint/parser",
  parserOptions: {
    sourceType: "module",
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.eslint.json"],
  },
  plugins: ["@typescript-eslint"],
  extends: [
    "eslint:recommended",
    "prettier/@typescript-eslint",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
  ],
}

Update The key part of the fix is this:更新修复的关键部分是:

parser: "@typescript-eslint/parser"

and this:和这个:

parserOptions: {
    sourceType: "module",
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.eslint.json"], // could be tsconfig.json too
}

Don't forget to include that file or file's directory in the include array of tsconfig.json.不要忘记将该文件或文件的目录包含在 tsconfig.json 的include数组中。

  1. Run npm i -D @types/node运行npm i -D @types/node

  2. Include typescript support for *.config.js files:包括对*.config.js文件的 typescript 支持:

tsconfig.json : tsconfig.json

{
  "include": [
    "**/*.config.js" // for *.config.js files
  ]
}

And then reload your IDE!然后重新加载你的 IDE!

Simply instruct eslint to ignore them by adding the ignorePatterns option to your .eslintrc.js :只需通过将ignorePatterns选项添加到您的.eslintrc.js来指示eslint忽略它们:

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: {
    project: './tsconfig.json',
  },
  ignorePatterns: ["babel.config.js"],
  ...

As there is not much value in linting your project config files, you can safely ignore them.由于 linting 项目配置文件没有太大价值,您可以放心地忽略它们。

Also, I would not make them part of your tsconfig.json or create a special tsconfig.eslint.json file, just for linting purpose.此外,我不会让它们成为您的tsconfig.json的一部分,也不会创建一个特殊的tsconfig.eslint.json文件,仅用于 linting 目的。

The error means there is a file which can be compiled but doesnt belong to currently defined project structure.该错误意味着有一个可以编译但不属于当前定义的项目结构的文件。 there are several solutions:有几种解决方案:

If you dont care if the file is compliled (ie. config files and such)如果您不关心文件是否已编译(即配置文件等)

  • add the filename to .eslintignore将文件名添加到.eslintignore

or或者

  • add the file or file pattern to .eslintrc.js file将文件或文件模式添加到.eslintrc.js文件
ignorePatterns: ["babel.config.js"]
// or 
ignorePatterns: ["**/*.config.js"]

You want the file to compiled您希望文件编译

add the file, folder or pattern to tsconfig.json file将文件、文件夹或模式添加到tsconfig.json文件

include": [
    "src",
    "other_src",
]

NOTE some changes needs IDE restart to take effect NOTE部分改动需要IDE重启生效

Simply add below code inside the.eslintrc.js file.只需在 .eslintrc.js 文件中添加以下代码。

ignorePatterns: ['.eslintrc.js']

For me the problem originates in some files ignored in tsconfig using the exclude property, yet eslint does not ignore them.对我来说,问题源于tsconfig中使用exclude属性忽略的一些文件,但eslint并没有忽略它们。

Usually if one wants that TSC will ignore files, then the same will be applied to eslint.通常,如果希望 TSC 忽略文件,那么同样适用于 eslint。 so just copy paste the value of exclude in .tsconfig configuration into the ignorePatterns property in .eslintrc configuration.所以只需将.eslintrc配置中的exclude值复制粘贴到.tsconfig配置中的ignorePatterns属性中。

I use a symlink from my project folder point to my home folder:我使用从我的项目文件夹指向我的主文件夹的符号链接:

/opt/project/workspace => ~/worspace /opt/project/workspace => ~/worspace

Opening the VSCode using the symlink path ~/workspace the error always persists.使用符号链接路径~/workspace打开 VSCode,错误始终存在。

Opening VSCode using the original path: /opt/project/workspace solves the problem.使用原始路径打开 VSCode: /opt/project/workspace即可解决问题。

This shouldn't be a problem, but for now it is.这不应该是一个问题,但现在它是。

I was having the same issue when adding '@typescript-eslint/prefer-nullish-coalescing': 'error' to .eslintrc.js .'@typescript-eslint/prefer-nullish-coalescing': 'error'添加到.eslintrc.js时,我遇到了同样的问题。

After a lot of trial and error, I came up with this solution, which works well in my case:经过大量的试验和错误,我想出了这个解决方案,在我的情况下效果很好:

module.exports = {
  ...
  overrides: [
    {
      files: ['*.ts', '*.tsx'],
      parserOptions: {
        // this setting is required to use rules that require type information
        project: './tsconfig.json',
      },
      rules: {
        '@typescript-eslint/prefer-nullish-coalescing': 'error',
      },
    },
  ],
}

Does this Error come from the VSCode Extension?此错误是否来自 VSCode 扩展? Does the Error have a relative Path that goes to the top level even though its in the same folder (or subfolder) (as shown in the error message below)?即使错误位于同一文件夹(或子文件夹)中(如下面的错误消息所示),错误是否有一个指向顶层的相对路径?

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: ../../../../../home/<ommitted>/projects/floor-planner/app/src/App.vue.
The file must be included in at least one of the projects provided.

If so, check if it also persists when using the CLI.如果是这样,请检查它在使用 CLI 时是否仍然存在。

From your project root you could run eslint.从你的项目根目录你可以运行eslint. . . If those errors are not found, you very likely have the project opened through a symlink .如果没有找到这些错误,您很可能通过 symlink打开了项目。

As of Aug 2021, the Eslint Extension does not work with Symlinks.自 2021 年 8 月起,Eslint 扩展不适用于符号链接。

From your project directory, run pwd -P to get the absolute Path.在您的项目目录中,运行pwd -P以获取绝对路径。

In my instance this results in在我的例子中,这导致

~/projects/floor-planner/app: pwd -P
/media/projects/2021-Q3/floor-planner/app

Open the Project through this Path.通过此路径打开项目。

Eslint should no longer show the Error. Eslint 不应再显示错误。

In our case we have multiple.eslintrc and multiple tsconfig.json in our directory tree.在我们的例子中,我们的目录树中有多个.eslintrc 和多个 tsconfig.json。 (One each for the server at . , and one each for the React client at ./client .) (每个用于.的服务器,一个用于./client的 React 客户端。)

This worked fine with the CLI but not with VS Code's plug-in.这适用于 CLI,但不适用于 VS Code 的插件。

The fix was to set the./client/.eslintrc project value to be relative to the root - not to the.eslintrc file.修复方法是将 ./client/.eslintrc project值设置为相对于根目录,而不是相对于 .eslintrc 文件。 After changing it from "./tsconfig.json" to "./client/tsconfig.json" the IDE linting works as expected.将其从“./tsconfig.json”更改为“./client/tsconfig.json”后,IDE linting 按预期工作。

I had this issue in VsCode when I was opening my Project too high up in the Folder Structure.当我在文件夹结构中打开我的项目太高时,我在 VsCode 中遇到了这个问题。 A weird solution but it worked.一个奇怪的解决方案,但它奏效了。

In my example, Firestore functions project for express in.ts, I had to open it from the functions folder it created.在我的示例中,Express in.ts 的 Firestore 函数项目,我必须从它创建的函数文件夹中打开它。

I should have noticed it was this sort of problem since 'npm run-script lint' never returned an error itself.我应该注意到这是这种问题,因为“npm run-script lint”本身从未返回错误。

I needed just to add the path to the new tsconfig.json in the project array and eslint script and the issue went away.我只需要在project数组和 eslint 脚本中添加新的tsconfig.json的路径,问题就消失了。

tsconfig.json : tsconfig.json

project: ["./newfolder/tsconfig.json"]

package.json : package.json

"eslint": "eslint -c ./.eslintrc.js './newfolder/**/*.ts'"

if in tsconfig.json include could not find any ts, and only get js files, this will show up as well如果在 tsconfig.json include找不到任何 ts,并且只获取 js 文件,这也会显示

I spent a lot of time on a problem like this one.我花了很多时间来解决这样的问题。

I had created a jest.config.ts instead of jest.config.js so it was throwing this exact eslint error我创建了一个jest.config.ts而不是jest.config.js所以它抛出了这个确切的 eslint 错误

I ran into this issue today, and none of the above solutions helped.我今天遇到了这个问题,以上解决方案都没有帮助。 The issue I had was that I had two files in the same directory that shared the same file name (sans extension).我遇到的问题是我在同一个目录中有两个文件共享相同的文件名(无扩展名)。 Eg, src/foo.ts and src/Foo.tsx .例如, src/foo.tssrc/Foo.tsx Renaming one of the files fixed the linter error.重命名其中一个文件修复了 linter 错误。 See @typescript-eslint/parser#955 .请参阅@typescript-eslint/parser#955

My issue was in PHPStorm is that I was having the working director set:我在 PHPStorm 中遇到的问题是我设置了工作主管:

在此处输入图像描述

I cleared that and everything worked:\我清除了,一切正常:\

for me, this issue happens when the project listed in parserOptions.project extends the base tsconfig.json which excludes this file.对我来说,当parserOptions.project中列出的项目扩展了排除此文件的基础 tsconfig.json 时,就会发生此问题。

removing extends: './tsconfig.json' from the project listed in parserOptions.project or removing exclude from the base config fixes it.从 parserOptions.project 中列出的项目中删除extends: './tsconfig.json' parserOptions.project从基本配置中删除exclude修复它。 but this workaround is not practical as we need to extend the base config.但是这种解决方法并不实用,因为我们需要扩展基本配置。

overriding the property exclude will perfectly help.覆盖属性exclude将完全有帮助。

tsconfig.spec.json tsconfig.spec.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./dist",
    "types": ["jest"]
  },
  "include": ["**/*.spec.ts"],
  "exclude": [""]
}

After putting lots of efforts and trials in correcting this error.I got to know my issue is i saved the ignore file as eslintignore instead of.eslintignore.经过大量努力和尝试来纠正这个错误。我知道我的问题是我将忽略文件保存为 eslintignore 而不是.eslintignore。

So due to this it was not able to ignore the.js, .d.ts files as dist got ignored.因此,由于这个原因,它无法忽略 .js、.d.ts 文件,因为 dist 被忽略了。

I happen to have the same problem for a while and then after adding .html to the include array of my tsconfig.json file, the problem has been fixed.我碰巧有一段时间遇到同样的问题,然后将.html添加到我的tsconfig.json文件的include数组后,问题已得到解决。

Since I am working on an Angular project, in the files array inside the overrides you may see my components extension which you may need to change as your project necessity.由于我正在处理 Angular 项目,因此在覆盖内的文件数组中,您可能会看到我的组件扩展名,您可能需要根据项目需要进行更改。

in the eslintrc.json file you need to have the following configuration:eslintrc.json文件中你需要有以下配置:

    "extends": [
        "plugin:@typescript-eslint/eslint-recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:@typescript-eslint/recommended-requiring-type-checking",
        "plugin:@angular-eslint/recommended"
    ],
    "overrides": [
        {
            "files": ["*.ts", "*.component.html", "*.component.ts"],
            "parserOptions": {
                "project": ["./tsconfig.json"],
                "extraFileExtensions": [".html"]
            },
            "rules": { "@typescript-eslint/prefer-nullish-coalescing": "error" }
        }
    ],
    "parser": "@typescript-eslint/parser",

make sure that you have the following line in your tsconfig.json file:确保您的tsconfig.json文件中有以下行:

"include": ["**/*.d.ts", "**/*.ts", "**/*.html", "**/*.scss", "**/*.css"],

Linux - My projects directory /home/user/projects was a symlink to /usr/share/dev/projects. Linux - 我的项目目录 /home/user/projects 是 /usr/share/dev/projects 的符号链接。 I removed the symlink and everything works now.我删除了符号链接,现在一切正常。

The error I was getting was telling me it couldn't find any.tsx file in "../../../../../{app name/path}"我得到的错误是告诉我它在“../../../../../{app name/path}”中找不到任何.tsx 文件

I hope my hours of troubleshooting helps someone:)我希望我的故障排除时间可以帮助某人:)

Despite assurances that typescript definitely, absolutely, never caches anything, I suspected this was a caching issue after, when getting annoyed at incessant typescript errors while debugging and developing a feature, I modified my tsconfig.json to exclude my src directory temporarily.尽管保证 typescript 绝对,绝对,从不缓存任何东西,但我怀疑这是一个缓存问题,因为在调试和开发功能时对不断的 typescript 错误感到恼火,我修改了我的 tsconfig.Z466DEEC76ECDF5FCA6D3857 目录暂时exclude了我的rcD1。 Upon reverting the change, and restarting my typescript server running through WebpackDevServer, I had this error, and got rid of it by doing yarn cache clean to clear my yarn cache, as well as rm -rf node_modules to delete my node modules folder.还原更改并重新启动通过 WebpackDevServer 运行的 typescript 服务器后,我遇到了这个错误,并通过执行yarn cache clean清除我的纱线缓存以及rm -rf node_modules删除我的节点模块文件夹来摆脱它。 Then I re installed my packages with yarn and when I ran my devserver again, typescript/tslint no longer showed this error.然后我用yarn重新安装了我的包,当我再次运行我的 devserver 时,typescript/tslint 不再显示这个错误。

I have this proble,when I update ts version to the latest我有这个问题,当我更新ts版本到最新

I had this error thrown at me even with the project settings included.即使包含项目设置,我也遇到了这个错误。 It was a vue sfc and the solution was a missing <script></script> template.这是一个vue sfc,解决方案是缺少<script></script>模板。

Set parserOptions in.eslintrc as below在.eslintrc 中设置parserOptions如下

"parserOptions": {
  "ecmaFeatures": {
    "jsx": true
  },
  "ecmaVersion": 2019,
  "sourceType": "module"        
},

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我已经为 @typescript-eslint/parser 设置了“parserOptions.project”,专门用于 .test.ts 文件? - Why am I getting "parserOptions.project" has been set for @typescript-eslint/parser specifically for .test.ts files? &#39;解析错误:已为@typescript-eslint/parser 设置了“parserOptions.project”是什么意思。 IntelliJ IDE 系列中的错误? - What means 'Parsing error: “parserOptions.project” has been set for @typescript-eslint/parser.' error in IntelliJ IDEs family? 我的“.eslintrc.js”文件出错 - “解析错误:已为@typescript-eslint/parser 设置了“parserOptions.project”。 - Error with my ".eslintrc.js" file - "Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser." 解析错误:“parserOptions.project”已为@typescript-eslint/parser 设置。 该文件与您的项目配置不匹配:jest.config.js - Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. The file does not match your project config: jest.config.js 解析错误:已为 @typescript-eslint/parser 设置“parserOptions.project”。 该文件与您的项目配置不匹配:.eslintrc.js - Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. The file does not match your project config: .eslintrc.js 如何修复“解析错误:“parserOptions.project”已为 @typescript-eslint/parser 设置。 在 gatsby-config 和 Gatsby-node.js 中 - How fix 'Parsing error: “parserOptions.project” has been set for @typescript-eslint/parser.' inside gatsby-config and Gatsby-node.js 为 ESLint 和 Airbnb typescript 设置配置 parserOptions 或解析器 package - Configure parserOptions or parser package for ESLint and Airbnb typescript setup (ESLint/赛普拉斯): 解析错误: ESLint 被配置为在 ` 上运行<tsconfigrootdir> /component/TestComponent.cy.ts` 使用 `parserOptions.project`</tsconfigrootdir> - (ESLint/Cypress): Parsing error: ESLint was configured to run on `<tsconfigRootDir>/component/TestComponent.cy.ts` using `parserOptions.project` 使用 Prettier Eslint 时找不到模块“@typescript-eslint/parser” - Cannot find module '@typescript-eslint/parser' when using Prettier Eslint 为什么 eslint 考虑 JSX 或一些反应 @types undefined,因为将 typescript-eslint/parser 升级到版本 4.0.0 - Why eslint consider JSX or some react @types undefined, since upgrade typescript-eslint/parser to version 4.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM