0

Similar question to How do you setup local environment variables for Cloud Functions for Firebase, but specifically looking how to setup environment variables that differ between development and production.

For example, Say I would typically have:

.env

REACT_APP_DOMAIN: https://foo-bar.web.app/

.env.development

REACT_APP_DOMAIN: http://localhost:3000/

How would one create a similar setup for Firebase Cloud Functions?

Nil
  • 99
  • 1
  • 1
  • 9
  • Are you using the Firebase emulator for developement? – Dharmaraj Jul 14 '21 at 16:18
  • Yes, I'm using Firebase emulator for development. – Nil Jul 14 '21 at 16:19
  • Have you tried [this](https://stackoverflow.com/questions/44766536/how-do-you-setup-local-environment-variables-for-cloud-functions-for-firebase) ? – Dharmaraj Jul 14 '21 at 16:20
  • @Dharmaraj can you be more specific? That's the question I linked to in my original question. As far as I noticed, nothing there explains how one would differentiate between the environments. To be clear, I have no problem setting variables that are accessible in both environments. My problem is that I need a single variable to be different based on the environment. – Nil Jul 14 '21 at 16:23

1 Answers1

1

I have an answer to my own question, though I don't think it's optimal.

In the Firebase's local-emulator documentation, they have this section:

Set up functions configuration (optional)

If you're using custom functions configuration variables, first run the command to get your custom config (run this within the functions directory) in your local environment:

firebase functions:config:get > .runtimeconfig.json

If using Windows PowerShell, replace the above with:

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

Though poorly explained, this is just creating a json file .runtimeconfig.json, which contains your firebase config variables. The variables in this file are then used by the Firebase emulator, in place of what is stored in Firestore. This is better explained in this github repo ticket.

Now knowing this, .runtimeconfig.json can be used in a similar manner that .env.development is. Instead of using the command they suggest, you can just manually update it as you need (or use their command to pull down your firebase variables and then modify them as needed).

You access/use those local variables in the exact same way you would normally access Firebase variables, functions.config().foo.bar.

When deployed, they'll reference your variables as set on Firebase. When in development (through firebase emulator), they'll reference what you have in .runtimeconfig.json.

Nil
  • 99
  • 1
  • 1
  • 9
  • If you find the Firebase documentation to be lacking, please consider clicking the "Send Feedback" button at the end of the appropriate page and providing constructive/actionable feedback to the docs team. – Greg Fenton Dec 20 '21 at 04:35