Fixing The ‘FirebaseApp name already exists!’ Error

To be honest, I have no idea why this is not inbuilt in Firebase, or even documented in the Firebase. But if you’re ever developing a Firebase app with hot reloading — every time you save — your code will try to re-initialize Firebase again, resulting in the following error;
FirebaseApp name [DEFAULT] already exists!Now, technically you can simply ignore this error and forge ahead, but if you want to, here’s a way to prevent this double initializing from happening.
import * as firebase from 'firebase/app'if ( !firebase.apps.length ) firebase.initializeApp( config )Essentially, it is checking if there is already a firebase app running and only initializing if there isn’t one. This works if you’re using Firebase Web v8 and below.
If you’re using Firebase Web v9 — which is highly recommended as it introduces a modular approach, allowing you to reduce the build size and efficiency — then you’ll need to tweak the code slightly.
import { initializeApp, getApps } from 'firebase/app'if ( !getApps().length ) initializeApp( config )As there is no longer a default firebase call, you’ll have to import getApps() first to check for any existing Firebase apps.
I’ve also realized that the new Web v9 sometimes requires you to store the initialized app into a variable so that it can be called or dropped into other Firebase functions. This is how I’ve been doing it.
import { initializeApp, getApps, getApp } from 'firebase/app'const app = !getApps().length ? initializeApp( config ) : getApp()If there is no initialized app, I’ll initialize it and store it into an app variable. Otherwise, I’ll use getApp() to get the initialized app instance and store it.
As always, I’m noting all these down as a reference to myself in the future, but I hope it helps someone who faced the same issue. Cheers!






