【问题标题】:Parsing error: Cannot read file '.../tsconfig.json'.eslint解析错误:无法读取文件'.../tsconfig.json'.eslint
【发布时间】:2021-03-04 02:37:57
【问题描述】:

错误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,清除缓存,但均无济于事。我猜我需要更改一些路径,但我不太擅长更改配置文件,所以我不想意外破坏整个项目。

【问题讨论】:

    标签: node.js typescript eslint typescript-eslint


    【解决方案1】:

    默认情况下,projects(在parserOptions 中)相对于当前工作目录进行解析。如果您在与包含tsconfig.json 的文件夹不同的工作目录中运行eslint,@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
    

    this question

    【讨论】:

    • 它修复了所有文件警告,但 .eslint 仍然出现新问题:解析错误:已为 @typescript-eslint/parser 设置了“parserOptions.project”。该文件与您的项目配置不匹配:.eslintrc.js。该文件必须包含在至少一个提供的项目中
    • @dz1002 这是因为@typescript-eslint/eslint-plugin 试图在.eslintrc.js 文件上使用类型感知规则,因此它需要一个TypeScript 项目。您可以禁用文件的这些规则,也可以使用 "include": ["**/*.ts", "**/*.js"]"allowJs": true 为 ESLint 添加 tsconfig。
    • module.exports 代码位于子模块的eslintrc.js 文件中,任何人都想知道。
    • 如果你有一个 .eslintrc.yml 文件,有没有办法做到这一点?
    【解决方案2】:

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

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

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

    【讨论】:

    • 如果您的根文件夹中有以下目录:“backend”和“frontend”,这将起作用:“eslint.workingDirectories”:[“backend”,“frontend”]
    • 对于 Firebase 云函数,请使用 "eslint.workingDirectories": ["functions"]
    【解决方案3】:

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

    "project": "PROJECT_NAME/tsconfig.json"

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

    【讨论】:

    • 这是一个简单直接的解决方案!!!
    • 这对我有用。
    【解决方案4】:

    另一种方法是,如果您碰巧打开了项目上方一层的 Workspace 文件夹(在我的情况下),只需使用 ACTUAL 项目文件夹启动一个新的 Workspace。这解决了我的 Eslint 错误。

    【讨论】:

      【解决方案5】:

      如果您在 IntelliJ 中的多模块项目 中收到以下错误消息(例如,包含多个后端服务和前端模块的文件夹),IntelliJ 可能无法判断要做什么做。

      在我的情况下,我在 .ts-file 顶部收到以下错误:

      为了解决这个问题,我点击了“ESLint settings...”并选择了“Manual ESLint configuration”

      然后我在“工作目录:”中输入 前端模块根 文件夹的路径,并在“ESLint 包”中选择了正确的 eslint 路径(在我的情况下,它位于 @987654326 @)。

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

      【讨论】:

        【解决方案6】:

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

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

        如果您在 vs code 中打开测试文件夹,然后导航到 testproject,如下所示

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

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

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

        【讨论】:

          【解决方案7】:

          对于那些使用 Angular 和 eslintrc.json 的人,将 2 星添加到项目节点,如下所示:

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

          【讨论】:

            【解决方案8】:

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

            【讨论】:

              【解决方案9】:

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

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

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

              【讨论】:

                【解决方案10】:

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

                已通过在 VS Code 中安装 Remote - WSL 扩展来修复。

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

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

                【讨论】:

                  【解决方案11】:

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

                  【讨论】:

                  • 请将您的代码直接发布到帖子中,无需添加额外的额外网址,将来可能会失效。
                  猜你喜欢
                  • 2022-11-11
                  • 2021-12-22
                  • 1970-01-01
                  • 2019-01-08
                  • 2017-10-11
                  • 2019-08-01
                  • 2021-06-04
                  • 2018-07-09
                  • 2021-03-16
                  相关资源
                  最近更新 更多