简体   繁体   English

我的“.eslintrc.js”文件出错 - “解析错误:已为@typescript-eslint/parser 设置了“parserOptions.project”。

[英]Error with my ".eslintrc.js" file - "Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser."

ESLint seems to be unable to parse a ".eslintrc.js" file . ESLint 似乎无法解析“.eslintrc.js”文件

Steps to reproduce: I set up a new "hello world" TypeScript project as follows:重现步骤:我建立了一个新的“hello world”TypeScript 项目,如下所示:

# Make a new directory for our new project
mkdir test
# Go into the new directory
cd test
# Create a "package.json" file
npm init --yes
# Install TypeScript
npm install typescript --save-dev
# Install ESLint (the linter)
npm install eslint --save-dev
# Install the Airbnb ESLint config (the most popular linting config in the world)
npm install eslint-config-airbnb-typescript --save-dev
# The import plugin for ESLint is needed for the Airbnb config to work properly with TypeScript
npm install eslint-plugin-import@^2.22.0 --save-dev
# The TypeScript plugin for ESLint is needed for the Airbnb config to work properly with TypeScript
npm install @typescript-eslint/eslint-plugin@^4.2.0 --save-dev
# Create the config file for TypeScript
touch tsconfig.json
# Create the config file for ESLint
touch .eslintrc.js
# Create the entry point for our TypeScript application
touch main.ts

I fill the "tsconfig.json" file with the following (a blank/default config):我用以下(空白/默认配置)填充“tsconfig.json”文件:

{}

I fill the ".eslintrc.js" file with the following, as specified in the Airbnb documentation :我在“.eslintrc.js”文件中填写以下内容,如Airbnb文档中所述

module.exports = {
  extends: ['airbnb-typescript/base'],
  parserOptions: {
    project: './tsconfig.json',
  },
};

I fill the "main.ts" with the following:我用以下内容填充“main.ts”:

const foo = 'bar';

Then, when I run npx eslint main.ts , it correctly generates the following error:然后,当我运行npx eslint main.ts ,它会正确生成以下错误:

  1:7  error  'foo' is assigned a value but never used  @typescript-eslint/no-unused-vars

Thus, ESLint appears to be working correctly.因此,ESLint 似乎工作正常。 However, when I run npx eslint .eslintrc.js , I get the following error:但是,当我运行npx eslint .eslintrc.js ,出现以下错误:

  0:0  error  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

This error also appears in VSCode whenever I open the ".eslintrc.js" file.每当我打开“.eslintrc.js”文件时,VSCode 中也会出现此错误。 The error needs to be solved so that ESLint can lint the rest of the file.需要解决该错误,以便 ESLint 可以对文件的其余部分进行 lint。 (To clarify, I want the ".eslintrc.js" file to be linted in the same way that I want my TypeScript source code to be linted - eg have 2 space indentation and so forth.) (澄清一下,我希望“.eslintrc.js”文件以与我希望我的 TypeScript 源代码被 lint 相同的方式被 linted - 例如有 2 个空格缩进等等。)

Additional info: I am on Windows 10 LTSC with Node v14.8.0 and npm version 6.14.7.附加信息:我使用的是带有 Node v14.8.0 和 npm 版本 6.14.7 的 Windows 10 LTSC。

ESLint seems to be unable to parse a ".eslintrc.js" file . ESLint 似乎无法解析“.eslintrc.js”文件

Steps to reproduce: I set up a new "hello world" TypeScript project as follows:重现步骤:我建立了一个新的“hello world”TypeScript 项目如下:

# Make a new directory for our new project
mkdir test
# Go into the new directory
cd test
# Create a "package.json" file
npm init --yes
# Install TypeScript
npm install typescript --save-dev
# Install ESLint (the linter)
npm install eslint --save-dev
# Install the Airbnb ESLint config (the most popular linting config in the world)
npm install eslint-config-airbnb-typescript --save-dev
# The import plugin for ESLint is needed for the Airbnb config to work properly with TypeScript
npm install eslint-plugin-import@^2.22.0 --save-dev
# The TypeScript plugin for ESLint is needed for the Airbnb config to work properly with TypeScript
npm install @typescript-eslint/eslint-plugin@^4.2.0 --save-dev
# Create the config file for TypeScript
touch tsconfig.json
# Create the config file for ESLint
touch .eslintrc.js
# Create the entry point for our TypeScript application
touch main.ts

I fill the "tsconfig.json" file with the following (a blank/default config):我用以下(空白/默认配置)填充“tsconfig.json”文件:

{}

I fill the ".eslintrc.js" file with the following, as specified in the Airbnb documentation :我在“.eslintrc.js”文件中填写以下内容,如Airbnb文档中所述

module.exports = {
  extends: ['airbnb-typescript/base'],
  parserOptions: {
    project: './tsconfig.json',
  },
};

I fill the "main.ts" with the following:我用以下内容填充“main.ts”:

const foo = 'bar';

Then, when I run npx eslint main.ts , it correctly generates the following error:然后,当我运行npx eslint main.ts ,它会正确生成以下错误:

  1:7  error  'foo' is assigned a value but never used  @typescript-eslint/no-unused-vars

Thus, ESLint appears to be working correctly.因此,ESLint 似乎工作正常。 However, when I run npx eslint .eslintrc.js , I get the following error:但是,当我运行npx eslint .eslintrc.js ,出现以下错误:

  0:0  error  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

This error also appears in VSCode whenever I open the ".eslintrc.js" file.每当我打开“.eslintrc.js”文件时,VSCode 中也会出现此错误。 The error needs to be solved so that ESLint can lint the rest of the file.需要解决该错误,以便 ESLint 可以对文件的其余部分进行 lint。 (To clarify, I want the ".eslintrc.js" file to be linted in the same way that I want my TypeScript source code to be linted - eg have 2 space indentation and so forth.) (澄清一下,我希望“.eslintrc.js”文件以与我希望我的 TypeScript 源代码被 lint 相同的方式被 linted - 例如有 2 个空格缩进等等。)

Additional info: I am on Windows 10 LTSC with Node v14.8.0 and npm version 6.14.7.附加信息:我使用的是带有 Node v14.8.0 和 npm 版本 6.14.7 的 Windows 10 LTSC。

Retsam's answer works perfectly well. Retsam 的回答非常有效。 However, you could also only use @typescript-eslint/parser for TypeScript files by using ESLint's overrides .但是,您也可以只使用 @typescript-eslint/parser 通过使用ESLint 的 overrides来处理 TypeScript 文件。 This avoids having to create a new tsconfig.json just for ESLint.这避免了创建一个新的tsconfig.json只是ESLint。

module.exports = {
  // Common configuration for .eslintrc.js and TypeScript files
  rules: {
    eqeqeq: "error",
    "no-var": "error",
    // ...
  },
  overrides: [{
    files: "**/*.ts"
    // TypeScript-only configuration
    parser: "@typescript-eslint/parser",
    parserOptions: {
      project: "tsconfig.json",
      tsconfigRootDir: __dirname,
      sourceType: "module",
    },
    plugins: ["@typescript-eslint"],
    rules: {
      "@typescript-eslint/no-floating-promises": "error",
      // ...
    }
  ]},
}

暂无
暂无

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

相关问题 解析错误:已为 @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 设置。 该文件与您的项目配置不匹配: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 &#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? 如何修复“解析错误:“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 解析错误:“parserOptions.project”已为 @typescript-eslint/parser 设置为 gatsby/typescript - Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser for gatsby/typescript “parserOptions.project” 已为 @typescript-eslint/parser 设置 - "parserOptions.project" has been set for @typescript-eslint/parser 为什么我已经为 @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? (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` parserOptions inside.eslintrc.js 中的 'project' 的作用是什么? - What is the role of the 'project' in parserOptions inside .eslintrc.js? typescript 中的 eslint 解析错误,lint 没有得到 parserOptions 配置 - Parsing error with eslint in typescript, lint doesnt get the parserOptions config
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM