首页 > 解决方案 > Node 8 Firebase 函数无法从 CloudBuild 部署

问题描述

我有一个项目使用 Node 8 引擎从我的本地开发环境成功部署到 firebase 功能到 firebase。我一直在尝试让它与 CloudBuild 一起使用,但是,由于使用了 async 关键字,我得到了一个语法错误:

/workspace/functions/lib/index.js:13
Step #5: app.get('XXXXXXXXX', async (req, resp) => {
Step #5: ^
Step #5: 
Step #5: SyntaxError: Unexpected token (
Step #5: at createScript (vm.js:56:10)
Step #5: at Object.runInThisContext (vm.js:97:10)
Step #5: at Module._compile (module.js:549:28)
Step #5: at Object.Module._extensions..js (module.js:586:10)
Step #5: at Module.load (module.js:494:32)
Step #5: at tryModuleLoad (module.js:453:12)
Step #5: at Function.Module._load (module.js:445:3)
Step #5: at Module.require (module.js:504:17)
Step #5: at require (internal/module.js:20:19)
Step #5: at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:21:11

表明节点引擎不支持 async/await,即不支持 Node 8。引擎在 package.json 中设置并在我的本地环境中工作。

"engines": {
    "node": "8"
  }

部署使用的是文档中描述的 firebase 容器:

- name: 'gcr.io/[PROJECT_NAME]/firebase'
  args: [ 'deploy', '-P', 'prod', '-f', '--only', 'functions','--token', '${_FIREBASE_TOKEN}']

CloudBuild 是否可以为不支持 Node8 的 Firebase 功能部署到不同的/内部端点并因此静默降级?

或者还有什么我错过的?

我试图通过将 .tsconfig 更改为目标 es2015 而不是 es2017 来证明这一理论,并从 package.json 中删除了引擎部分。结果是将函数部署到 Node8 函数?

Step #5: deploying functions
Step #5: functions: ensuring necessary APIs are enabled...
Step #5: functions: all necessary APIs are enabled
Step #5: functions: preparing functions directory for uploading...
Step #5: functions: packaged functions (91.42 KB) for uploading
Step #5: functions: functions folder uploaded successfully
Step #5: functions: updating Node.js 8 function XXXXXX(us-central1)...
Step #5: functions[XXXXXXX(us-central1)]: Successful update operation.
Step #5:
Step #5: Deploy complete!

我认为这是因为 node 8 函数已经存在,所以它只是一个更新而不是创建,但可能表明代码解析器是基于 node6 的,并且在通过 cloudbuild 部署时忽略了引擎设置?

标签: firebasegoogle-cloud-functionsgoogle-cloud-build

解决方案


Firebase 工具将解析您的代码以查找函数触发器名称,这似乎就是它出错的地方。

我的猜测是您的自定义容器正在运行与 ES2017/ES8关键字gcr.io/[PROJECT_NAME]/firebase不兼容的 Node 版本。async/await

您需要确保您的容器至少运行 Node v7.6.0。

希望这可以帮助。


推荐阅读