Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firebase App named '[DEFAULT]' already exists (app/duplicate-app) #1999

Closed
martinvarelaaaa opened this issue May 17, 2017 · 10 comments
Closed

Comments

@martinvarelaaaa
Copy link

martinvarelaaaa commented May 17, 2017

I started a project based on https://github.com/zeit/next.js/tree/v3-beta/examples/with-firebase

The error I have when importing firebase in more than one component.

In this firebase start file:

`import firebase from 'firebase'

const firebaseConfig = {
apiKey: "fdsfsdfdsf",
authDomain: "fdsfdsfsdfdsf",
databaseURL: "sdfdsfdsf",
projectId: "dsfdsfdsf",
storageBucket: "dsfdsfdsf",
messagingSenderId: "dsfdsfsdfdsf"
}

const FbApp = firebase.initializeApp(firebaseConfig)

export default FbApp.auth()`

Then in the components:

import firebase from '../lib/firebaseClient'

With a single component works well, but if I add a new component with import firebase from '../lib/firebaseClient' the application fail:

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

@kennylavender
Copy link

kennylavender commented May 17, 2017

You can check firebase.apps to see if its loaded. If your only loading it once, then you can just check the length. If you have multiple then you could check each apps name.

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

http://stackoverflow.com/questions/37652328/how-to-check-if-a-firebase-app-is-already-initialized-on-android

@martinvarelaaaa
Copy link
Author

The solution:

`import firebase from 'firebase'

try {
firebase.initializeApp({
databaseURL: 'dfgdfg'
})
} catch (err) {
// we skip the "already exists" message which is
// not an actual error when we're hot-reloading
if (!/already exists/.test(err.message)) {
console.error('Firebase initialization error', err.stack)
}
}

const fb= firebase
export default fb`

@AnsarSamad
Copy link

the issue is due to calling the initialize method of firebase more than multiple times . me had this same issue . and i could fix it by restricting the initialize method call more than once . make the firebase configurations in one class and make it as singleton class .

@gitKendra
Copy link

gitKendra commented Aug 28, 2017

I am new to firebase and a recent web developer. I had this issue. My issue was caused by linking my javascript file in both the head and in the body of my html file. I specifically had both firebase and js script tags in the html head and another js script tag at the end of the body tag. My fix was to delete all script tags from the head and put them at the bottom of the body.

@Purii
Copy link

Purii commented Sep 3, 2017

That's my solution:

// 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());

@kylops
Copy link

kylops commented Sep 27, 2017

Brilliant solution! @Purii

@francisribeiro
Copy link

@Purii You just saved my day

@jide
Copy link

jide commented Mar 7, 2018

If you use firestore, use :

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

@abdelinh0
Copy link

@jide thanks

I had to also include import 'firebase/firestore';

import * as firebase from 'firebase'
import 'firebase/firestore';

const config = {
  apiKey: "***",
  authDomain: "***",
  databaseURL: "***",
  projectId: "***",
  storageBucket: "***",
  messagingSenderId: "***"
};

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

@brunokiafuka
Copy link

@Purii and @jide , thanks for the solutions

@lock lock bot locked as resolved and limited conversation to collaborators May 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants