简体   繁体   English

Typescript / Eslint 在我开始的每个项目中都会抛出“无法读取文件 tsconfig.json”

[英]Typescript / Eslint throws "Cannot read file tsconfig.json" in every project I start

In other threads I read that the issue might come when eslint and tsconfig.json are not in the same working folder, but I have all the files in the project's root folder.在其他线程中,我读到当 eslint 和 tsconfig.json 不在同一个工作文件夹中时可能会出现问题,但我在项目的根文件夹中拥有所有文件。 Somehow, after initializing the project, the thrown error seems to try to find tsconfig.json in the parent folder of the project (so, outside the project):不知何故,在初始化项目后,抛出的错误似乎试图在项目的父文件夹中找到 tsconfig.json(所以,在项目之外):

Let's say the project is in: '\parentFolder\ProjectRoot' tsconfig is in the route: '\parentFolder\ProjectRoot\tsconfig.json假设项目位于:'\parentFolder\ProjectRoot' tsconfig 在路径中:'\parentFolder\ProjectRoot\tsconfig.json

The eslint error I get (at the top of index.ts) is the following:我得到的 eslint 错误(在 index.ts 的顶部)如下:

Parsing error: Cannot read file ' parentFolder \tsconfig.json'解析错误:无法读取文件“ parentFolder \tsconfig.json”

  • Note that eslint is somehow looking for tsconfig in the wrong folder (the parent of the root folder)请注意,eslint 以某种方式在错误的文件夹(根文件夹的父文件夹)中寻找 tsconfig


The contents of \parentFolder\ProjectRoot are: \parentFolder\ProjectRoot 的内容是:

The steps to get to this point have been:达到这一点的步骤是:

  • npm init npm 初始化
  • npm i -D typescript npm i -D typescript
  • add "tsc": "tsc" script to package.json将“tsc”:“tsc”脚本添加到 package.json
  • npm run tsc -- --init npm 运行 tsc -- --init
  • npm i express npm 我快递
  • npm i -D eslint @types/express @typescript-eslint/eslint-plugin @typescript-eslint/parser npm i -D eslint @types/express @typescript-eslint/eslint-plugin @typescript-eslint/parser
  • npm i -D ts-node-dev npm i -D ts-node-dev

.eslintrc: .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 tsconfig.json

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

package.json (looks like changing index.js to index.ts in "main" does nothing) 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 (I get implicity errors here in req and res, I imagine caused by the fact that tsconfig can't be found). 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}`);
});


edit:编辑:

I had typescript installed globally (and also as a dev dependency in these projects).我在全球范围内安装了 typescript(也作为这些项目中的开发依赖项)。 Thinking this might be the issue, I uninstalled the global version of typescript.考虑到这可能是问题所在,我卸载了 typescript 的全球版本。

When checking all global packages with npm list -g --depth 0, I get the following:使用 npm list -g --depth 0 检查所有全局包时,我得到以下信息:

在此处输入图像描述

I don't know if that would be related to the issue.我不知道这是否与这个问题有关。

What worked for me was on the.eslintrc of the folder you are on to add:对我有用的是您要添加的文件夹的.eslintrc:

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

I'm putting this answer here as an idiot-proofing measure.我把这个答案放在这里是为了防止白痴。 This can also happen if you open the project in the wrong folder.如果您在错误的文件夹中打开项目,也会发生这种情况。 I got this error when I opened the parent directory of the project instead of the project directory itself.当我打开项目的父目录而不是项目目录本身时出现此错误。 I had a project in GitLab -> projectFolder and opened GitLab.我在GitLab -> projectFolder中有一个项目并打开了 GitLab。 I got the error saying tsconfig.json could not be read.我收到错误消息说tsconfig.json无法读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析错误:无法读取文件'.../tsconfig.json'.eslint - Parsing error: Cannot read file '.../tsconfig.json'.eslint Typescript 无法使用 tsconfig.json - Typescript not working with tsconfig.json Typescript Nodejs Dockerfile:错误 TS5057:在指定目录中找不到 tsconfig.json 文件:'.' - Typescript Nodejs Dockerfile: error TS5057: Cannot find a tsconfig.json file at the specified directory: '.' TypeScript编译器忽略tsconfig.json中的声明文件 - TypeScript compiler ignores declaration file in tsconfig.json TypeScript tsconfig.json 中的路径别名未找到 - TypeScript path aliases in tsconfig.json not found 打字稿错误在 tsconfig.json 中将模块更改为“umd”或“amd”时找不到模块“typescript-Collections” - Typescript error Cannot find module 'typescript-Collections' when changing the module to “umd” or “amd” in tsconfig.json tsc忽略了​​我的tsconfig.json文件 - tsc ignores my tsconfig.json file tsconfig.json中文件夹中的相对文件路径 - Relative file path in folders in tsconfig.json TypeScript tsconfig.json文件中使用typeRoots与baseUrl的相对路径 - relative path using typeRoots vs. baseUrl in TypeScript tsconfig.json file 我应该在.gitignore文件中写jsconfig.json或tsconfig.json吗? - should i write jsconfig.json or tsconfig.json in .gitignore file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM