Parsing error: Cannot read file '.../tsconfig.json'.eslint

Parsing error: Cannot read file '.../tsconfig.json'.eslint

错误 Parsing error: Cannot read file '.../tsconfig.json'.eslint 显示在 src 文件夹中的所有 .ts 文件中,包括 index.ts

我不知道如何设置配置。该问题仅显示一条红线并使文件变红。但是,一切都可以编译并且 运行 很好。整个 Node 项目是使用 firebase CLI 创建的。

tsconfig.json 文件:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

.eslintrc.js 文件:

module.exports = {
  env: {
    browser: true,
    es6: true,
    node: true,
  },
  extends: [
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: "tsconfig.json",
    sourceType: "module",
  },
  plugins: [
    "@typescript-eslint",
    "import",
  ],
  rules: {
    "@typescript-eslint/adjacent-overload-signatures": "error",
    "@typescript-eslint/no-empty-function": "error",
    "@typescript-eslint/no-empty-interface": "warn",
    "@typescript-eslint/no-floating-promises": "error",
    "@typescript-eslint/no-namespace": "error",
    "@typescript-eslint/no-unnecessary-type-assertion": "error",
    "@typescript-eslint/prefer-for-of": "warn",
    "@typescript-eslint/triple-slash-reference": "error",
    "@typescript-eslint/unified-signatures": "warn",
    "comma-dangle": "warn",
    "constructor-super": "error",
    eqeqeq: ["warn", "always"],
    "import/no-deprecated": "warn",
    "import/no-extraneous-dependencies": "error",
    "import/no-unassigned-import": "warn",
    "no-cond-assign": "error",
    "no-duplicate-case": "error",
    "no-duplicate-imports": "error",
    "no-empty": [
      "error",
      {
        allowEmptyCatch: true,
      },
    ],
    "no-invalid-this": "error",
    "no-new-wrappers": "error",
    "no-param-reassign": "error",
    "no-redeclare": "error",
    "no-sequences": "error",
    "no-shadow": [
      "error",
      {
        hoist: "all",
      },
    ],
    "no-throw-literal": "error",
    "no-unsafe-finally": "error",
    "no-unused-labels": "error",
    "no-var": "warn",
    "no-void": "error",
    "prefer-const": "warn",
  },
  settings: {
    jsdoc: {
      tagNamePreference: {
        returns: "return",
      },
    },
  },
};

我曾尝试重新启动 VScode、清除缓存,但都无济于事。我猜我需要更改一些路径,但我不太擅长更改配置文件,所以我不想不小心破坏整个项目。

默认情况下,projects(在parserOptions中)是相对于当前工作目录解析的。如果您 运行 eslint 在与包含 tsconfig.json 的文件夹不同的工作目录中,@typescript-eslint/parser 将无法找到该文件。

要解决此问题,您可以将 tsconfigRootDir 设置为 __dirname,这将使解析器解析相对于 .eslintrc.js:

的项目配置
module.exports = {
  // ...
  parserOptions: {
    project: "tsconfig.json",
    tsconfigRootDir: __dirname,
    sourceType: "module",
  },
  // ...
}

如果您遇到问题

/path/to/.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

参见

一种 VSCode 特定的方法,对我有用的是在根项目目录中创建一个 .vscode 文件夹,并将以下 属性 添加到 settings.json其中的文件:

{
  "eslint.workingDirectories": [
    "src"
  ]
}

"src" 可以是任何目录,它应该在 eslint 的范围内。

使用以下行更新您的 eslintrc.json 文件:

"project": "PROJECT_NAME/tsconfig.json"

其中 PROJECT_NAME 是您的项目名称(不是宏)。

另一种方法是,如果您碰巧打开了项目上方一级的工作区文件夹(就像我的情况一样),只需使用 ACTUAL 项目文件夹启动一个新的工作区。这解决了我的 Eslint 错误。

如果您在 IntelliJ多模块项目(例如包含多个后端服务和前端模块的文件夹)中收到以下错误消息,则 IntelliJ 可能不是能够知道该做什么。

在我的例子中,我在 .ts 文件的顶部遇到了以下错误:

为了解决这个问题,我点击了“ESLint 设置...”并选择了“手动 ESLint 配置”

然后我在“工作目录:”中输入 前端模块根目录 文件夹的路径,并在“ESLint 包”中选择正确的 eslint 路径(在我的例子中- 在 <frontend-module-root>/node_modules/eslint).

这样我仍然可以使用 eslint 并且根本不需要更改任何与项目相关的配置(它已经在 CLI 中的前端模块根文件夹中工作)。

解析错误:无法读取文件'd:\test\testproject\tsconfig.app.eslint.json'.eslint 由于在 vs 代码中打开项目文件夹的方式,我在这里遇到的主要问题之一。

假设文件夹结构是 d:\test\testproject 其中“testproject”是您正在使用的 angular 项目。

如果您在 vs 代码中打开测试文件夹,然后导航到测试项目,如下所示

D:\test>
D:\test>cd testproject
D:\test\testproject> 

这样做会创建 2 个 settings.json 文件,一个用于打开的主文件夹,一个用于子文件夹,这反过来又不允许正确应用设置。

因此,始终直接在 vscode 中打开项目文件夹将有助于解决问题,当在 eslintrc.json 和 tsconfig.json 文件中正确提及所有依赖项时,将有助于解决问题问题。

对于那些使用 Angular 和 eslintrc.json 的人,像这样将 2 星添加到项目节点:

"overrides": [
    {
        "files": ["*.ts"],
        "parserOptions": {
            "project": ["**/tsconfig.json"],
        },

我突然开始使用 vscode 看到这个错误。重新启动 vscode 已解决我的问题

对我来说解决这个问题的方法是简单地导航到包含我的 tsconfig.json 的项目目录,并且它能够找到该文件。

否则,我喜欢@dheeraj9499 的回答,您可以在“parserOptions”中修改您的 .eslintrc.json“项目”数组,并从当前目录中找到您的 tsconfig.json 所在的位置。所以像下面这样的东西能够在正确的目录中找到 tsconfig.json:

"parserOptions": {
  "project": [
    "code/eMOS/Scripts/tsconfig.json",
    "code/eMOS/Scripts/tsconfig.spec.json"
  ],
 }

我在 Windows 10 上通过 WSL 2 处理打字稿项目时在 Visual Studio 代码中遇到此错误。

已通过在 VS Code 中安装 Remote - WSL 扩展来解决此问题。

这些文件托管在我的 \wsl$\Ubuntu\home\ 位置。错误消息指定了正确的文件位置,VS Code 可以打开文件。

以上使用根目录和解析器选项进行的修复没有帮助。

我通过在扩展中添加 @typescript-eslint/parser 来解决它: