Parsing error: Cannot read file '\tsconfig.json' eslint after following Firebase Cloud Functions initialization instructions

Parsing error: Cannot read file '\tsconfig.json' eslint after following Firebase Cloud Functions initialization instructions

问题

在 VSCode 中我的 TypeScript 项目初始化后,使用 firebase 工具在 official documentation 之后编写 Firebase Cloud Functions,index.ts 文件的第一行显示错误:

Parsing error: Cannot read file '\tsconfig.json' eslint [1,1]

并且 .eslintrc.js 显示错误:

File is a CommonJS module; it may be converted to an ES6 module.ts(80001)

由于所有文件都是自动生成的,所以这些错误完全出人意料,我想摆脱它们。

版本

作为记录,以下是安装的版本:

npm      --version 8.1.3
tsc      --version 4.4.4
node     --version 17.0.1
firebase --version 9.22.0

安装过程

这些是我在 VSCode 的 powershell 中使用的一些命令 info/warnings:

>npm install -g firebase-tools
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
...

>firebase init
...
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: undefined,
npm WARN EBADENGINE   required: { node: '14' },
npm WARN EBADENGINE   current: { node: 'v17.0.1', npm: '8.1.3' }
npm WARN EBADENGINE }
...

>npm install firebase-functions@latest firebase-admin@latest --save
...
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
...

>firebase deploy
...
C:\Users\SAMS\firebase_dogout\functions\src\index.ts
  1:13  warning  'functions' is defined but never used  @typescript-eslint/no-unused-vars
...
Error: functions predeploy error: Command terminated with non-zero exit code2

文件

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 = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
    "google",
    "plugin:@typescript-eslint/recommended",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    project: ["tsconfig.json", "tsconfig.dev.json"],
    sourceType: "module",
  },
  ignorePatterns: [
    "/lib/**/*", // Ignore built files.
  ],
  plugins: [
    "@typescript-eslint",
    "import",
  ],
  rules: {
    "quotes": ["error", "double"],
    "import/no-unresolved": 0,
  },
};

index.ts

import * as functions from "firebase-functions";

export const helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

我试过的

  1. 重新初始化
  2. 正在创建包含 settings.json 文件的 .vscode 文件夹
 {
   "eslint.workingDirectories": [
       "src" // and "functions"
   ]
 }
  1. 正在使用以下行更新您的 eslintrc.json 文件:"project":"PROJECT_NAME/tsconfig.json"
  2. 通过在 parserOptions
  3. 中设置 tsconfigRootDir: __dirname 来更新 .eslintrc.js
  4. 正在删除 ESLint 扩展。错误消失了,但是 firebase deploy 命令不允许代码部署。

所以, 并没有真正帮助

好的,在这个 github 线程的帮助下我已经解决了这个问题 False positive Error - TS6133 error (declared but its value is never read) report.

我已将 tsconfig.json 文件中的 "noUnusedLocals" 设置从默认的 true 更改为 false,因此文件变为:

"compilerOptions": {
    ...
    "noUnusedLocals": false,
    ...
  }

然而,奇怪的是,在使用此设置成功将功能部署到 Firebase Cloud Fuctions 并随后还原更改后,重新部署没有显示任何错误并且也成功。

更新

重试几次后,我必须承认解决方案有所不同。 事实证明 确实有帮助,特别是@cherryblossom 解决方案。在 .eslintrc.js 文件中设置 tsconfigRootDir: __dirname 并重新启动 VSCode 即可解决问题。

.eslintrc.js:

module.exports = {
  // ...
  parserOptions: {
    ...
    tsconfigRootDir: __dirname,
    ...
  },
  // ...
}

以下步骤对我有用。我不确定它们是否都是必要的,但最终我到达了我想去的地方。此处前面提到的步骤对我的情况没有太大帮助,可能是因为可能有许多原因导致相同的错误消息。寻找解决方案有时是一个反复试验的过程,直到找到解决方案。

就我而言

  1. 我在 nodejs.org/en/download.

    更新了我的 nodejs

    nodejs.org/en/download

然后我重新加载并重新初始化了我的 Firebase 函数项目。然后我更新了打字稿解析器。

npm i @typescript-eslint/parser

终于它似乎起作用了。我会祈祷这两天痛苦的过程确实结束了。

我看了报错信息,研究死了。 即使我在 firebase 中设置了一个基本项目并尝试 运行 它仍然弹出。 最后在绝望中我从我的

中删除了 COMMENTED 一行

eslintrc.js

project: ["tsconfig.json", "tsconfig.dev.json"], //通过注释删除 出来

所以我的 .eslintrc.js 文件正在读取 作为

root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
    "google",
    "plugin:@typescript-eslint/recommended",
  ],
  parser: "@typescript-eslint/parser",
  parserOptions: {
    // project: ["tsconfig.json", "tsconfig.dev.json"],
    sourceType: "module",
  },
  ignorePatterns: [
    "/lib/**/*", // Ignore built files.
  ],
  plugins: [
    "@typescript-eslint",
    "import",
  ],
  rules: {
    "quotes": ["error", "double"],
        "import/no-unresolved": 0,
      },
    };


And just to make sure I was not out to lunch 
I cleaned up the index.ts file as per recommedations and then I ran it...


PS C:\ac\x> firebase deploy --only "functions:helloWorld"
=== Deploying to '***********'...
i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> eslint --ext .js,.ts .
Running command: npm --prefix "$RESOURCE_DIR" run build
> build
> tsc
+  functions: Finished running predeploy script.
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
+  functions: required API cloudfunctions.googleapis.com is enabled
+  functions: required API cloudbuild.googleapis.com is enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged C:\ac\x\functions (87.87 KB) for uploading
+  functions: functions folder uploaded successfully
i  functions: creating Node.js 16 function helloWorld(us-central1)...
+  functions[helloWorld(us-central1)] Successful create operation.
Function URL (helloWorld(us-central1)): https://us-central1-xxxxxxx-xx.cloudfunctions.net/helloWorld
i  functions: cleaning up build files...
  • 部署完成!

我在这上面浪费了很多时间,我希望就此结束....