【问题标题】:Typescript / Eslint throws "Cannot read file tsconfig.json" in every project I startTypescript / Eslint 在我开始的每个项目中都会抛出“无法读取文件 tsconfig.json”
【发布时间】:2021-03-16 04:11:37
【问题描述】:

在其他线程中,我读到当 eslint 和 tsconfig.json 不在同一个工作文件夹中时可能会出现问题,但我在项目的根文件夹中拥有所有文件。不知何故,初始化项目后,抛出的错误似乎试图在项目的父文件夹中找到tsconfig.json(所以,在项目之外):

假设项目位于:'\parentFolder\ProjectRoot' tsconfig 在路径中:'\parentFolder\ProjectRoot\tsconfig.json

我得到的 eslint 错误(在 index.ts 的顶部)如下:

解析错误:无法读取文件 'parentFolder\tsconfig.json'

  • 请注意,eslint 以某种方式在错误文件夹(根文件夹的父文件夹)中查找 tsconfig


\parentFolder\ProjectRoot 的内容是:

达到这一点的步骤是:

  • npm 初始化
  • npm i -D 打字稿
  • 在 package.json 中添加 "tsc": "tsc" 脚本
  • npm run tsc -- --init
  • npm i express
  • npm i -D eslint @types/express @typescript-eslint/eslint-plugin @typescript-eslint/parser
  • npm i -D ts-node-dev

.eslintrc:

{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking"
  ],
  "plugins": ["@typescript-eslint"],
  "env": {
    "browser": true,
    "es6": true
  },
  "rules": {
    "@typescript-eslint/semi": ["error"],
    "@typescript-eslint/explicit-function-return-type": 0,
    "@typescript-eslint/no-unused-vars": [
        "error", { "argsIgnorePattern": "^_" }
    ],
     "@typescript-eslint/no-explicit-any": 1,
    "no-case-declarations": 0
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": "./tsconfig.json"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ES6",
    "outDir": "./build/",
    "module": "commonjs",
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "esModuleInterop": true
  }
}

package.json(看起来在“main”中将 index.js 更改为 index.ts 没有任何作用)

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "tsc": "tsc",
    "dev": "ts-node-dev index.ts",
    "lint": "eslint --ext .ts ."
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/express": "^4.17.9",
    "@typescript-eslint/eslint-plugin": "^4.9.0",
    "@typescript-eslint/parser": "^4.9.0",
    "eslint": "^7.14.0",
    "ts-node-dev": "^1.0.0",
    "typescript": "^4.1.2"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

index.ts (我在 req 和 res 中得到隐含错误,我想是由于找不到 tsconfig 的事实造成的)。

const express = require('express');
const app = express();app.use(express.json());
const PORT = 3000;
app.get('/ping', (_req, res) => {
  res.send('pong');
});
app.listen(PORT, () => {
  console.log(`Server running on port ${PORT}`);
});


编辑:

我在全球范围内安装了 typescript(并且还作为这些项目中的开发依赖项)。考虑到这可能是问题所在,我卸载了全球版本的 typescript。

使用 npm list -g --depth 0 检查所有全局包时,我得到以下信息:

我不知道这是否与问题有关。

【问题讨论】:

  • parentFolder中有.eslintrcpackage.json吗?
  • 没有。在与“ProjectRoot”相同的父文件夹中还有另一个项目,它有自己的 .eslintrc 和 tsconfig.json。在这两种情况下,eslint 都会在包含两个项目的父文件夹中查找 tsconfig 文件。我认为在本地和全局安装 typescript 可能是个问题,但我已经卸载了全局版本,但我仍然遇到同样的问题。
  • 尝试在.eslintrc 中将./tsconfig.json 更改为tsconfig.json
  • 能否请您添加这些文件的代码而不是屏幕截图,以便可以在自己的机器上运行?
  • 将 ./tsconfig 更改为 tsconfig 不起作用。为您添加了代码(刚刚学会了轻松粘贴代码块,对此感到抱歉)

标签: node.js typescript express eslint


【解决方案1】:

对我有用的是您要添加的文件夹的 .eslintrc:

parserOptions: {
   project: 'tsconfig.json',
   tsconfigRootDir: __dirname // <-- this did the trick for me
}

【讨论】:

    猜你喜欢
    • 2021-03-04
    • 2022-11-11
    • 1970-01-01
    • 2021-12-22
    • 2020-11-20
    • 2020-09-09
    • 1970-01-01
    • 2022-10-19
    • 2019-01-08
    相关资源
    最近更新 更多