【问题标题】:Firebase-Admin package Typescript error in Cloud Functions Firestore : @types/googlemapsCloud Functions Firestore 中的 Firebase-Admin 包 Typescript 错误:@types/googlemaps
【发布时间】:2018-05-13 09:36:25
【问题描述】:

我有两个具有类似 Cloud Functions 设置的项目,都直接使用 Typescript 设置(无 Webpack),类似于 this examplethis one

其中一个使用 Firestore,另一个不使用。不使用 Firestore 的编译部署没有错误。

但是,带有 Firestore 功能的那个在 tsc 编译时给了我这个错误:

../node_modules/@types/googlemaps/index.d.ts(33,29): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(37,19): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(54,28): error TS2304: Cannot find name 'Node'.
../node_modules/@types/googlemaps/index.d.ts(787,30): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(798,36): error TS2304: Cannot find name 'Node'.
../node_modules/@types/googlemaps/index.d.ts(811,26): error TS2304: Cannot find name 'Node'.
../node_modules/@types/googlemaps/index.d.ts(1135,20): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1136,22): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1137,18): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1138,22): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1139,23): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1140,23): error TS2304: Cannot find name 'Element'.
../node_modules/@types/googlemaps/index.d.ts(1141,29): error TS2304: Cannot find name 'Element'.

... and goes on.

这些是 package.json 依赖项:

"dependencies": {
    "@google-cloud/storage": "^1.5.0",
    "axios": "^0.17.1",
    "child-process-promise": "^2.2.1",
    "firebase-admin": "~5.5.1",
    "firebase-functions": "^0.7.3"
  },
  "devDependencies": {
    "typescript": "^2.6.2"
  },

tsconfig的内容:

{
  "compilerOptions": {
    "lib": ["es6", "es2015.promise"],
    "module": "commonjs",
    "noImplicitAny": false,
    "outDir": "build",
    "sourceMap": true,
    "target": "es6"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

我错过了什么?它与打字稿版本有关吗? (2.6) 我需要导入@types 吗?添加开发依赖@types/node 没有帮助。

【问题讨论】:

    标签: typescript firebase google-cloud-functions google-cloud-firestore


    【解决方案1】:

    起初我认为问题是在tsconfig 文件中排除了node_modules 文件夹并删除了"exclude": [ "node_modules" ] 部分。它没有帮助。

    然后,由于所有错误似乎都与 DOM 元素名称或“节点”有关,它应该与某些通用包的缺少类型定义有关,因此对此事进行了另一次搜索并遇到了类似问题的答案: Typescript build getting errors from node_modules folder

    像这样更改tsconfig(添加对lib.es6.d.ts 的引用)让我的问题消失:

      "include": [
        "src/**/*.ts"
      ],
      "files": [
        "node_modules/typescript/lib/lib.es6.d.ts"
      ],
      "exclude": [
          "node_modules"
      ]
    

    【讨论】:

    • 我已经为此苦苦挣扎了很久,您帮我解决了,非常感谢!
    • 这是有效的,但是当调用new google.maps.LatLngit 时会在运行时出现错误ReferenceError google is not defined@Bogac
    【解决方案2】:

    添加 "skipLibCheck": true 到 tsconfig.json 也可以解决问题。

    信用 - https://github.com/firebase/firebase-tools/issues/749#issuecomment-385693352

    【讨论】:

    • 但是为什么呢?
    【解决方案3】:

    有类似的问题:

    node_modules/@google-cloud/firestore/types/firestore.d.ts:1629:8 - 错误 TS2304:找不到名称“AsyncIterable”。

    对我来说,更新(从 3.12 到 3.13)firebase-functions 很有帮助。

    【讨论】:

    • 是的,只需将 firebase-admin 和 firebase-functions 更新到最新版本
    【解决方案4】:

    我在 Firebase 函数项目中遇到了这个问题。 完整解决方案

    node_modules/@google-cloud/firestore/types/firestore.d.ts:1629:8 - 错误 TS2304:找不到名称“AsyncIterable”。

    1. 在您的项目文件夹中,在 cmd 中运行:npm install -g npm-check-updates
    2. 在您的项目文件夹中,转到 /functions 并在 cmd 中运行:ncu -u 在这里您会看到类似这样的内容:
     firebase-admin            ^9.2.0  →   ^9.5.0
     firebase-functions       ^3.11.0  →  ^3.13.1
     googleapis               ^40.0.0  →  ^67.0.0
     typescript                ^3.8.0  →   ^4.1.5
     firebase-functions-test   ^0.2.0  →   ^0.2.3
    
    1. 这里在 cmd 中运行:npm install
    2. 此时,您可以尝试再次部署,但可能会出现以下错误: node_modules/@google-cloud/firestore/types/firestore.d.ts:1638:8 - 错误 TS2583:找不到名称“AsyncIterable”。您需要更改目标库吗?尝试将 lib 编译器选项更改为“es2018”或更高版本。
    3. 如果您在上一步中遇到错误,请打开文件 functions/tsconfig.json 并更新 target 节点的值,如错误文本中所述,在我的情况下,我更新为 es2018 并且部署应该可以正常运行

    附言不要忘记保存每个更改,只需 Ctrl+S ;)

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 2018-11-29
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      • 2018-06-17
      • 2018-03-23
      • 1970-01-01
      相关资源
      最近更新 更多