【问题标题】:Parsing error: Cannot read file '\tsconfig.json' eslint after following Firebase Cloud Functions initialization instructions解析错误:遵循 Firebase Cloud Functions 初始化说明后无法读取文件 '\tsconfig.json' eslint
【发布时间】:2021-12-22 02:11:48
【问题描述】:

问题

在我的 TypeScript 项目在 VSCode 中初始化之后,在 official documentation 之后使用 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

安装过程

这些是我在 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

文件

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 中设置tsconfigRootDir: __dirname 来更新.eslintrc.js
  3. 删除 ESLint 扩展。错误消失了,但firebase deploy 命令不允许代码部署。

所以,related thread 并没有真正的帮助

【问题讨论】:

  • 您将文件托管在哪里?你能把这个文件放在Cloud storage 中并从那里读取它而不是让它发挥作用吗?
  • index.ts 文件在本地托管在 ../functions/src 中。也许我可以将此文件加载到 Cloud Storage 中,但我想将其加载到 Firebase Cloud Functions 以实现 Cloud Functions 功能(我不确定我是否理解您的建议)。

标签: typescript firebase google-cloud-functions eslint


【解决方案1】:

好的,在这个 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 并随后恢复更改后,重新部署并没有显示任何错误并且也成功了。

更新

重试几次后,我必须承认解决方案有所不同。 原来related stackoverflow thread 确实有帮助,尤其是@cherryblossom 解决方案。在.eslintrc.js文件中设置tsconfigRootDir: __dirname,重启VSCode即可解决问题。

.eslintrc.js:

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

【讨论】:

    猜你喜欢
    • 2021-03-04
    • 2022-11-11
    • 1970-01-01
    • 2023-03-02
    • 2018-09-16
    • 2019-07-09
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    相关资源
    最近更新 更多