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

Comments

@martinvarelaaaa
Copy link

@martinvarelaaaa 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

This comment has been minimized.

Copy link

@kennylavender 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

This comment has been minimized.

Copy link
Author

@martinvarelaaaa martinvarelaaaa commented May 19, 2017

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

This comment has been minimized.

Copy link

@AnsarSamad AnsarSamad commented May 27, 2017

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

This comment has been minimized.

Copy link

@gitKendra 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

This comment has been minimized.

Copy link

@Purii 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

This comment has been minimized.

Copy link

@kylops kylops commented Sep 27, 2017

Brilliant solution! @Purii

@francisribeiro

This comment has been minimized.

Copy link

@francisribeiro francisribeiro commented Mar 4, 2018

@Purii You just saved my day

@jide

This comment has been minimized.

Copy link

@jide jide commented Mar 7, 2018

If you use firestore, use :

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

This comment has been minimized.

Copy link

@isaacabdel isaacabdel commented Mar 9, 2018

@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

This comment has been minimized.

Copy link

@brunokiafuka brunokiafuka commented Mar 12, 2018

@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.
Projects
None yet
10 participants
You can’t perform that action at this time.