Typescript:更新 Firebase 函数依赖项时出现编译错误

Posted

技术标签:

【中文标题】Typescript:更新 Firebase 函数依赖项时出现编译错误【英文标题】:Typescript: compilation errors when updating Firebase function dependencies 【发布时间】:2020-01-06 12:34:17 【问题描述】:

我尝试更新我的 Firebase 函数项目的依赖项以使用更新版本的 firebase-functions 和 firebase-admin,而这又似乎需要更新版本的 TypeScript 和 tslint。这是我生成的 package.json:


  "name": "functions",
  "scripts": 
    "lint": "./node_modules/.bin/tslint -p tslint.json",
    "build": "./node_modules/.bin/tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  ,
  "main": "lib/index.js",
  "engines": 
    "node": "8"
  ,
  "dependencies": 
    "@types/jsonwebtoken": "^7.2.8",
    "axios": "^0.18.1",
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^3.1.0",
    "handlebars": "^4.1.2",
    "html-pdf": "^2.2.0",
    "js-sha256": "^0.9.0",
    "json2csv": "^4.1.2",
    "jsonwebtoken": "^8.3.0",
    "jwks-rsa": "^1.3.0",
    "moment": "^2.24.0",
    "pdfkit": "^0.9.1",
    "uuid": "^3.3.2",
    "validator": "^10.11.0"
  ,
  "devDependencies": 
    "tslint": "^5.12.0",
    "typescript": "^3.2.2"
  ,
  "private": true

但是现在,当我尝试部署我的 Firebase 函数时,我得到了一堆我什至无法理解的 Typescript 编译错误。有人知道这些是什么以及如何解决它们或解决它们吗?

Running command: npm --prefix $RESOURCE_DIR run build

> functions@ build /Users/sarbogast/dev/myproject/backend/functions
> tsc

node_modules/@google-cloud/storage/build/src/file.d.ts:35:29 - error TS2694: Namespace '"http"' has no exported member 'OutgoingHttpHeaders'.

35     extensionHeaders?: http.OutgoingHttpHeaders;
                               ~~~~~~~~~~~~~~~~~~~

node_modules/@types/node/index.d.ts:54:11 - error TS2300: Duplicate identifier 'IteratorResult'.

54 interface IteratorResult<T> 
             ~~~~~~~~~~~~~~

  node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6
    41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
            ~~~~~~~~~~~~~~
    'IteratorResult' was also declared here.

node_modules/typescript/lib/lib.es2015.iterable.d.ts:41:6 - error TS2300: Duplicate identifier 'IteratorResult'.

41 type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
        ~~~~~~~~~~~~~~

  node_modules/@types/node/index.d.ts:54:11
    54 interface IteratorResult<T> 
                 ~~~~~~~~~~~~~~
    'IteratorResult' was also declared here.


Found 3 errors.

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ build: `tsc`
npm ERR! Exit status 2
npm ERR! 
npm ERR! Failed at the functions@ build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/sarbogast/.npm/_logs/2019-09-03T14_57_53_748Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code2

【问题讨论】:

FWIW,有同样的问题。如果我找到任何东西,会通知您。 可能与github.com/firebase/firebase-functions/issues/551有关 关于这个问题的任何消息,我都开始了。 【参考方案1】:

我可以通过将 "typeRoots": ["node_modules/@types"] 添加到 tsconfig.json 中的 compilerOptions 来解决此问题。

我找到了这个解决方案here

【讨论】:

这为我解决了,而其他解决方案没有【参考方案2】:

这就是我设法解决问题的方法:我将@types/node 安装为显式的开发依赖项,这就足够了。这是我的 package.json 最终的样子:


  "name": "functions",
  "scripts": 
    "lint": "./node_modules/.bin/tslint -p tslint.json",
    "build": "./node_modules/.bin/tsc",
    "serve": "npm run build && firebase serve --only functions",
    "shell": "npm run build && firebase experimental:functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  ,
  "engines": 
    "node": "8"
  ,
  "main": "lib/index.js",
  "dependencies": 
    "@types/jsonwebtoken": "^7.2.8",
    "axios": "^0.18.1",
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "firebase-admin": "^8.5.0",
    "firebase-functions": "^3.2.0",
    "handlebars": "^4.1.2",
    "html-pdf": "^2.2.0",
    "js-sha256": "^0.9.0",
    "json2csv": "^4.1.2",
    "jsonwebtoken": "^8.3.0",
    "jwks-rsa": "^1.3.0",
    "moment": "^2.24.0",
    "pdfkit": "^0.9.1",
    "uuid": "^3.3.2",
    "validator": "^10.11.0"
  ,
  "devDependencies": 
    "@types/node": "^12.7.5",
    "tslint": "~5.8.0",
    "typescript": "^3.6.3"
  ,
  "private": true

【讨论】:

【参考方案3】:

指向以node_modules 开头并以.d.ts 结尾的文件的错误是打字稿lib check。这些旨在捕获已安装模块之间的类型冲突,但正确设置可能会很棘手。

我通常只是将其关闭,并获得更快编译时间的额外好处。

//tsconfig.json

    "compilerOptions": 
        "skipLibCheck": true,
        ...
    

【讨论】:

以上是关于Typescript:更新 Firebase 函数依赖项时出现编译错误的主要内容,如果未能解决你的问题,请参考以下文章

为啥在 Typescript(Firebase 函数)中双重导入 NPM 包

带有 TypeScript 的 Firebase Cloud Functions:实时数据库更新以成功结束但没有更新任何内容,JS 工作正常

firebase 云功能中带有 typescript 的 firebase-admin

使用 Typescript 创建 Firebase 云函数时出现“找不到名称‘ServiceWorkerRegistration’”错误

Vue + TypeScript + Firebase:如何键入包含 firebase `User` 的`ref` 的对象?

如何使用以 TypeScript for Firebase Realtime 数据库编写的云函数从 onCreate 触发以外的节点获取数据?