Google's Firebase is a great system and environment to quickly build web apps. However, there are a few tricky things to work around, such as deploying to different environments.

You can use Firebase’s env config variables,  but I find them difficult to manage. More than once I used prod variables in dev!

dotenv to the rescue

Here is what I do to create a staging and production environments with different configurations in Firebase.

1. Setup a unique Firebase project for each environment, e.g. Production, Staging, and Development.

2. At a command prompt, assign an alias for each project with:

firebase use --add

3. Create a different .env file for each environment: .env.production, .env.staging, .env.development. Place your specific configs for those environments there.

4. Install "dotenv" with:

npm i --save-dev dotenv

This is assuming your using NodeJS.

5. Add a build script "build-staging" to your package.json file (or build-dev):

"build-staging": "dotenv -e .env.staging react-scripts build"

6. Build with the script:

npm run build-staging

7. Deploy to Firebase:

firebase deploy -P staging

DONE!


Thank you for reading. 🙏

But before you go: if you're a Firebase user, you're probably concerned about a surprise month-end bill or being suddenly shut off if your free tier runs out.

If this resonates with you, check out our app 🔥fireRun.io🔥 - the only way to monitor and control your Firebase spend and usage. Get your FREE report with 14 usage and daily spend metrics tracked.