如何为 Firebase 的 Cloud 函数设置本地环境变量

我使用 http 云函数来侦听请求,然后返回一个简单的消息。

我正在本地开发云功能,使用:

firebase serve --only functions

我已经设置了一些自定义环境变量,使用

firebase functions:config:set

在部署项目时,使用下面的代码访问自定义配置变量可以很好地工作

 functions.config()

但是在本地开发时就不起作用了。当按下: http://localhost:5002/my-project-name/us-central1/functionName触发函数时,我无法访问自定义配置变量。在本地使用 Functions.config ()时,我可以看到默认配置,但不能看到自定义配置变量

在本地工作时,是否存在环境变量的替代解决方案或最佳实践?

36857 次浏览

As of now, you have to manually create a .runtimeconfig.json file inside your functions directory by running this command. Then run the serve command.

firebase functions:config:get > .runtimeconfig.json

If you are using Windows Powershell, replace the above with:

firebase functions:config:get | ac .runtimeconfig.json

You can learn more in https://firebase.google.com/docs/functions/local-emulator

For those who want to use the environment variables (process.env), I follow this workaround.

Set the config values before deploying

firebase functions:config:set envs.db_host=$DB_HOST_PROD envs.db_user=$DB_USER_PROD envs.db_password=$DB_PASSWORD_PROD envs.db_name=$DB_NAME_PROD envs.db_use_ssl=false

Read the config and update the env variables first thing under your functions code.

const functions = require('firebase-functions');
const config = functions.config();


// Porting envs from firebase config
for (const key in config.envs) {
process.env[key.toUpperCase()] = config.envs[key];
}

I am not sure if the top-rated answer works or not but for firebase function on mac (to-serve locally), I do something like this

npm run admin-keys && export dev=true && firebase emulators:start

Where admin keys is

"admin-keys": "export GOOGLE_APPLICATION_CREDENTIALS='./.keys/admin.keys.json'"

This will load configuration from .runtimeconfig.json

For production, you would manually have to set it by doing something like this

firebase functions:config:set facebookCred.secret="something"

You can keep a file called .env.json and load it when you trigger deploy command

{
"name": "project",
"version": "0.0.0",
"scripts": {
"deploy": "npm run env && firebase deploy --only functions",
"env": "test -f env.json && firebase functions:config:unset env && firebase functions:config:set env=\"$(cat env.json)\" || echo \"Please add the file env.json before deploy.\""
},
"dependencies": {
"firebase-functions": "^3.1.0"
},
"devDependencies": {
"firebase-functions-test": "^0.1.6"
}
}

I've narrowed down the issue to Windows Powershell.

Running firebase functions:config:get > .runtimeconfig.json in powershell generates a broken json I don't know why, which when parsed gives Unexpected token � in JSON at position 0.

I've managed to sort it out by running .runtimeconfig.json generation command in Windows command prompt.

If you are using Nrwl NX, you will have to generate your .runtimeconfig.json inside of the dist/apps/functions directory.

Example package.json:

{
"scripts": {
"firebase:emulators:start": "firebase functions:config:get > dist/apps/functions/.runtimeconfig.json && env-cmd firebase emulators:start --export-on-exit=\".firebase-emulator\" --import=\".firebase-emulator\""
}
}