1

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94

1 Answers1

2

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thanks Doug, you guys (Firebasers) have excellent support for questions on StackOverflow. It looks like for now, I do have to keep my naive solution? – Daniel Danielecki Nov 30 '20 at 20:36