简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM