Firebase App named '[DEFAULT]' already exists code example

Example 1: Firebase: Firebase App named '[DEFAULT]' already exists

if (!firebase.apps.length) {
   firebase.initializeApp({});
}else {
   firebase.app(); // if already initialized, use that one
}

Example 2: Firebase: Firebase App named '[DEFAULT]' already exists

if (!firebase.apps.length) {
    firebase.initializeApp({});
}

Example 3: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).

if (!firebase.apps.length) {
    firebase.initializeApp(firebaseConfig);
}

Example 4: firebaseError: Firebase: Firebase App named '[DEFAULT'] already exists (app/duplicate-app).

// Config file
import * as firebase from "firebase";

const config = {...};

export default !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();

// Other file
import firebase from '../firebase';
...
console.log(firebase.name);
console.log(firebase.database());

Example 5: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).

// Config file
import * as firebase from "firebase";

const config = {...};

export default !firebase.apps.length ? firebase.initializeApp(config) : firebase.app();

// Other file
import firebase from '../firebase';
...
console.log(firebase.name);