Firebase云功能节点8-远程部署时出现“错误:找不到环境配置”

时间:2019-09-16 21:41:28

标签: node.js typescript firebase google-cloud-functions

我正在用节点8的打字稿编写我的Firebase云函数。为了从云函数访问Firebase存储桶,我以以下方式在index.ts中初始化CF,以启用prod和dev之间的切换Firebase项目:

import * as admin from 'firebase-admin'
import * as geofunctions from 'geofirestore'

if(!process.env.FIREBASE_CONFIG){
    throw Error("Can't find env config")
}

const adminConfig = JSON.parse(process.env.FIREBASE_CONFIG)
const projectId = adminConfig.projectId

const certPath = projectId + '.json'
const bucketUrl = projectId + '.appspot.com'

admin.initializeApp({
    credential: admin.credential.cert(certPath),
    storageBucket: bucketUrl
});

这符合:

https://firebase.google.com/docs/storage/admin/start

这在本地运行时效果很好,例如:firebase -P dev serve

但是,当我使用firebase deploy -P dev --only functions进行远程部署时,它对所有功能都失败,并显示以下错误:

⚠  functions[<<MY FUNCTION NAME>>(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: Error: Can't find env config
    at Object.<anonymous> (/srv/lib/index.js:7:11)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at getUserFunction (/worker/worker.js:439:24)
    at loadUserCode (/worker/worker.js:495:18)

关键行是:Error: Can't find env config

当我删除process.env.FIREBASE_CONFIG并对身份验证详细信息进行硬编码时,它可以在远程正​​常运行。

作为一种解决方法,我正在使用process.env.GCLOUD_PROJECT来获取我的projectId-确实适用于远程部署。

我正在使用最新的Firebase依赖项-请参阅我的package.json

"dependencies": {
    "firebase-admin": "^8.5.0",
    "firebase-functions": "^3.2.0",
    "firebase-tools": "^7.3.2",
    ...
},
  "devDependencies": {
    "firebase-functions-test": "^0.1.6",
    "tslint": "^5.12.0",
    "typescript": "^3.6.3"
    ...
  },

有什么主意我可能做错了吗?

0 个答案:

没有答案