[英]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.