解析错误:无法读取文件'.../tsconfig.json'.eslint

新手上路,请多包涵

错误 Parsing error: Cannot read file '.../tsconfig.json'.eslint 全部显示 .ts 文件夹中的 src 文件,包括 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,清除缓存,但均无济于事。我猜我需要更改一些路径,但我不太擅长更改配置文件,所以我不想意外破坏整个项目。

原文由 darren z 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.7k
2 个回答

默认情况下, project s(在 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

看到 这个问题

原文由 cherryblossom 发布,翻译遵循 CC BY-SA 4.0 许可协议

这个对我有用

"parserOptions": {
  "project": ["<YourProjectName>/tsconfig.json"],
}

原文由 IglesiasNoelia 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题