解析错误:无法读取文件'…/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,清除缓存,但都无济于事。我猜我需要改变一些路径,但我不太擅长改变配置文件,所以我不想不小心破坏整个项目。


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

看这个问题。


一个特定于vcode的方法,对我来说是在根项目目录中创建一个.vscode文件夹,并将以下属性添加到设置中。Json文件在里面:

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

src可以是eslint作用域内的任何目录。


更新你的eslintrc。Json文件,包含以下行:

“项目”:“PROJECT_NAME / tsconfig.json”

使用PROJECT_NAME作为项目的名称(不是宏)。


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


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

在我的情况下,我得到了以下错误的顶部。ts文件:

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

然后我在“Working directories:”中输入了前端模块根文件夹的路径,并在“eslint包”中选择了正确的eslint路径(在我的情况下,是在< fronttend -module-root>/node_modules/eslint)。

通过这种方式,我仍然可以让eslint工作,而不需要改变任何与项目相关的配置(它已经从CLI中的前端模块根文件夹中工作)。


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

假设文件夹结构为 D:\test\testproject这里的“testproject”是你正在使用的angular项目。

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

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

这样做将创建2个设置。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进行typescript项目时,我在Visual Studio Code中得到了这个错误。

通过在VS Code中安装Remote - WSL扩展修复了这个问题。

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

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


我通过添加@typescript-eslint/parser来解决这个问题:


这对我很有用

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