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

AngularjsUnit TestingFirebaseKarma RunnerKarma Jasmine

Angularjs Problem Overview


Hi I am trying to unit test while developing a simple web with AngularJS + Firebase, but I have a problem defining the spec and trying the test runner

myProject/test/spec/main.js :

describe('Controller: MainCtrl', function() {

var MainCtrl, scope

beforeEach(module('MainApp'));

beforeEach(inject(function ($controller, $rootScope) {
	scope = $rootScope.$new();
	MainCtrl = $controller('MainCtrl', {
		$scope: scope
	})
}));

it('should....',function(){
	expect(true).toBe(true)
})
it('should2....',function(){
	expect(false).toBe(false)
})
});

result:

PhantomJS 2.1.1 (Mac OS X 0.0.0) Controller: MainCtrl should.... FAILED
Error: [$injector:modulerr] Failed to instantiate module MainApp due 
to: Firebase: Firebase App named '[DEFAULT]' already exists 
(app/duplicateapp).

However, defining the spec once is not a problem. For example:

it('should....',function(){
   expect(true).toBe(true)
})

I do not know why this is the reason I give a hint to me

Here is the project I am developing.

https://github.com/parkwookyun/firebase-angular-board

Angularjs Solutions


Solution 1 - Angularjs

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

Similar answer here

Solution 2 - Angularjs

I had the same issue using firebase, and I realized that I was initializing the firebase twice, example:

function initializeFirebase(){    
 var config = {
    apiKey: "myApiKey",
    authDomain: "myAuthDomain",
    databaseURL: "myDatabaseUrl",
    storageBucket: "myStorageBocket",
    messagingSenderId: "idhere"   
   };   
   //initialize firebase  
   firebase.initializeApp(config);  
}

In my case I was using react, And I was calling initializeFirebase() from two different components, but I've decided to call it from the parent component. And now I'm able to query my database, insert records, remove, etc.

Hope this helps someone

Solution 3 - Angularjs

i only glanced at your github repo to see that there is more .js files than suggested in your question. i had 2 separate .js files. both being included on the same html page that were both configuring and initializing the default app when it only needed to be done once for that page i believe. what worked for me was to comment out the second "initializeApp()" and i no longer got that error. try to look for the below in your .js and comment out or delete one of the initializations.

var config = {
      apiKey: "my api key",
      authDomain: "something.firebaseapp.com",
      databaseURL: "https://somesite.firebaseio.com",
      projectId: "myProjectId",
      storageBucket: "somesite.appspot.com",
      messagingSenderId: "someIntegers"
    };
//firebase.initializeApp(config);

Solution 4 - Angularjs

This error occurs simply because you are trying to initialize firebase multiple times into your project.

Solution 5 - Angularjs

I recently solved this issue in my own code. My issue was caused by accidentally linking my javascript file, which calls initializeApp(), in the head and in the body of my html file. My fix was to delete the duplicate javascript tag in the head of my html file so only one existed in the body.

Scan through your code to make sure you only call initializeApp() once. This includes any js files that might be calling the method or duplicating the js file in your html file.

Solution 6 - Angularjs

In my case, I was not explicitly calling initializeApp() more than once, so I was still confused after reading the main answers. Turns out that if you use Firebase hosting, then initialization is called automatically through SDK auto-configuration, i.e. when you have this line in index.html:<script src="/__/firebase/init.js"></script> you don't need to call initializeApp(config).

"When you deploy to Firebase or test your app locally, this script automatically configures the Firebase JavaScript SDK for the active Firebase project and initializes the SDK." SDK auto-configuration

Solution 7 - Angularjs

I have had the same problem

if you are using ES6 modules and you installed firebase by npm you can not put init scrips into index.html

it is duplications of firebase dependancies

Solution 8 - Angularjs

Check if project is in admin.apps. If it is, use if not initialize it. This will prevent the error from occurring.

const projectFbData = admin.apps.find((item) => item.name_ === projectId) || admin.initializeApp(config, projectId)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionparkwookyunView Question on Stackoverflow
Solution 1 - AngularjsIsaac SekamatteView Answer on Stackoverflow
Solution 2 - AngularjsErick GarciaView Answer on Stackoverflow
Solution 3 - AngularjsDuan WalkerView Answer on Stackoverflow
Solution 4 - AngularjsSaurabh JainView Answer on Stackoverflow
Solution 5 - AngularjskendraView Answer on Stackoverflow
Solution 6 - Angularjsbrownmagik352View Answer on Stackoverflow
Solution 7 - AngularjsJurajView Answer on Stackoverflow
Solution 8 - AngularjsJeremy TenjoView Answer on Stackoverflow