如何从Firebase中的.env文件读取环境变量? [英] How to read Environmental Variables from .env file in Firebase?
问题描述
存储环境变量的最简单,多平台的方法是在项目根目录中使用 .env
文件.
The easiest, multiplaform way to store environmental variables is by using .env
file in the root of our project.
现在,我面临的问题与Firebase有关,更确切地说,与Firebase的Cloud Functions有关.对于通过 process.env.SOME_VARIABLE
访问的所有环境变量,我得到的是 undefined
,直到将 .env
文件复制到我的部署文件夹之前.在这种情况下,它是默认的 functions
,只有这样,它才能起作用.
Now, the problem I'm facing is related to Firebase, more precisely Cloud Functions for Firebase. For all environmental variables accessed via process.env.SOME_VARIABLE
I'm getting undefined
, until the time of copying .env
file to my deployment folder. In this case it's the default's functions
, only then it works.
问题:是否有办法做到不复制 .env
文件?在所有我的( Angular
和 Angular Universal
)应用程序中,我都可以访问 process.env.SOME_VARIABLE
,但后端逻辑除外,该逻辑为100%依靠功能.
The question: is there any way to do so without duplication of the .env
file? Across all my (Angular
with Angular Universal
) app I can access the process.env.SOME_VARIABLE
, excluding the backend logic, which is 100% relying on functions.
我试图调试它
console.log(require('dotenv').config({ debug: true }));
在 Firebase
的日志中向我返回了 workspace/.env
之类的东西,但对此无能为力.
Returned me something like workspace/.env
in the Firebase
's logs, but couldn't do anything with that.
我的一些试验:
require('dotenv').config({ path: '/workspace/.env' });
require('dotenv').config({ path: join(process.cwd(), '.env') });
require('dotenv').config({ path: join(process.cwd(), './../.env') });
require('dotenv').config({ path: __dirname + './../.env' });
require('dotenv').config({ path: join(process.cwd() + './../.env') });
总是给我 undefined
,除非将 .env
手动复制到可部署的 functions
文件夹中.
Always gave me undefined
, unless the .env
was manually copied to the deployable functions
folder.
推荐答案
Firebase CLI会在部署时将所有内容部署在functions文件夹中,但node_modules除外.如果它不在此处进行部署,那么您将无法完全脱离主机读取到远程网络位置,而根本无法阅读它.当前尚无法使CLI合并到其他位置的文件中.您也许可以尝试符号链接.
The Firebase CLI deploys everything in the functions folder at the time of deployment, except for node_modules. If it's not in there for deployment, you won't be able to read it at all without somehow going off-host to a remote network location. There is currently no way to make the CLI merge in files from other locations. You could perhaps try a symlink.
除非将.env手动复制到可部署的功能文件夹中.
unless the .env was manually copied to the deployable functions folder.
这可能是您最终必须要做的.
This is probably what you'll have to end up doing.
这篇关于如何从Firebase中的.env文件读取环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!