![](/img/trans.png)
[英]Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. The file does not match your project config: .eslintrc.js
[英]Error with my ".eslintrc.js" file - "Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser."
ESLint 似乎無法解析“.eslintrc.js”文件。
重現步驟:我建立了一個新的“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
我用以下(空白/默認配置)填充“tsconfig.json”文件:
{}
我在“.eslintrc.js”文件中填寫以下內容,如Airbnb文檔中所述:
module.exports = {
extends: ['airbnb-typescript/base'],
parserOptions: {
project: './tsconfig.json',
},
};
我用以下內容填充“main.ts”:
const foo = 'bar';
然后,當我運行npx eslint main.ts
,它會正確生成以下錯誤:
1:7 error 'foo' is assigned a value but never used @typescript-eslint/no-unused-vars
因此,ESLint 似乎工作正常。 但是,當我運行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
每當我打開“.eslintrc.js”文件時,VSCode 中也會出現此錯誤。 需要解決該錯誤,以便 ESLint 可以對文件的其余部分進行 lint。 (澄清一下,我希望“.eslintrc.js”文件以與我希望我的 TypeScript 源代碼被 lint 相同的方式被 linted - 例如有 2 個空格縮進等等。)
附加信息:我使用的是帶有 Node v14.8.0 和 npm 版本 6.14.7 的 Windows 10 LTSC。
ESLint 似乎無法解析“.eslintrc.js”文件。
重現步驟:我建立了一個新的“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
我用以下(空白/默認配置)填充“tsconfig.json”文件:
{}
我在“.eslintrc.js”文件中填寫以下內容,如Airbnb文檔中所述:
module.exports = {
extends: ['airbnb-typescript/base'],
parserOptions: {
project: './tsconfig.json',
},
};
我用以下內容填充“main.ts”:
const foo = 'bar';
然后,當我運行npx eslint main.ts
,它會正確生成以下錯誤:
1:7 error 'foo' is assigned a value but never used @typescript-eslint/no-unused-vars
因此,ESLint 似乎工作正常。 但是,當我運行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
每當我打開“.eslintrc.js”文件時,VSCode 中也會出現此錯誤。 需要解決該錯誤,以便 ESLint 可以對文件的其余部分進行 lint。 (澄清一下,我希望“.eslintrc.js”文件以與我希望我的 TypeScript 源代碼被 lint 相同的方式被 linted - 例如有 2 個空格縮進等等。)
附加信息:我使用的是帶有 Node v14.8.0 和 npm 版本 6.14.7 的 Windows 10 LTSC。
Retsam 的回答非常有效。 但是,您也可以只使用 @typescript-eslint/parser 通過使用ESLint 的 overrides來處理 TypeScript 文件。 這避免了創建一個新的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.