How to read Environmental Variables from .env file in Firebase?

一曲冷凌霜 提交于 2021-02-04 08:28:39

问题


The easiest, multiplaform way to store environmental variables is by using .env file in the root of our project.

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.

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.

I've tried to debug it

console.log(require('dotenv').config({ debug: true }));

Returned me something like workspace/.env in the Firebase's logs, but couldn't do anything with that.

Some of my trials:

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') });

Always gave me undefined, unless the .env was manually copied to the deployable functions folder.


回答1:


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.

unless the .env was manually copied to the deployable functions folder.

This is probably what you'll have to end up doing.



来源:https://stackoverflow.com/questions/65080574/how-to-read-environmental-variables-from-env-file-in-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!