解析错误:按照 ​​Firebase Cloud Functions 初始化说明后无法读取文件“\tsconfig.json”eslint

Mic*_*Zur 32 firebase typescript eslint google-cloud-functions

问题

在我的 TypeScript 项目在 VSCode 中初始化后,使用 firebase 工具按照官方文档编写 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
Run Code Online (Sandbox Code Playgroud)

安装流程

这些是我在 VSCode 的 powershell 中使用的命令以及一些信息/警告:

>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
Run Code Online (Sandbox Code Playgroud)

文件

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017"
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}
Run Code Online (Sandbox Code Playgroud)

.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,
  },
};

Run Code Online (Sandbox Code Playgroud)

索引.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!");
});
Run Code Online (Sandbox Code Playgroud)

我尝试过的

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

所以,相关线程并没有真正帮助

Mic*_*Zur 57

好的,我已经在这个 github 线程的大力帮助下解决了这个问题\n误报错误 - TS6133 错误(已声明但其值从未被读取)报告

\n

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

\n
"compilerOptions": {\n    ...\n    "noUnusedLocals": false,\n    ...\n  }\n
Run Code Online (Sandbox Code Playgroud)\n

然而,奇怪的是,在成功部署函数以Firebase Cloud Fuctions使用此设置并恢复更改后,重新部署不会显示任何错误并且也成功。

\n

更新

\n

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

\n

.eslintrc.js:

\n
module.exports = {\n  // ...\n  parserOptions: {\n    ...\n    tsconfigRootDir: __dirname,\n    ...\n  },\n  // ...\n}\n
Run Code Online (Sandbox Code Playgroud)\n


Den*_*4RP 5

我读了错误消息并研究了它。即使我在 firebase 中设置了一个基本项目并尝试运行它之后,它仍然弹出。最后在绝望中我从我的注释中删除了一行

eslintrc.js

项目:[“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...
Run Code Online (Sandbox Code Playgroud)
  • 部署完成!

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