question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The default Firebase app already exists. This means you called initializeApp() more than once

See original GitHub issue

[REQUIRED] Environment info

firebase-tools: 7.1.0

Platform: Windows 10

[REQUIRED] Test case

import * as functions from 'firebase-functions';
import express from 'express';
import admin from "firebase-admin"
admin.initializeApp(functions.config())
console.log('Firebase Environment setup success')
const app1 = express()
app1.get("*", (request, response) => {
    response.send("bar")
})
const foo = functions.https.onRequest(app1)
module.exports = {
    foo
}

[REQUIRED] Steps to reproduce

Run firebase emulators:start --only functions visit the url of the function on your browser

[REQUIRED] Expected behavior

initializeApp should be called only once

[REQUIRED] Actual behavior

initializeApp is called everytime the url is called

! Error: The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name. at FirebaseAppError.FirebaseError [as constructor] (C:\Users\Business\PhpstormProjects\gantt-flexx\node-server\node_modules\firebase-admin\lib\utils\error.js:42:28) at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\Users\Business\PhpstormProjects\gantt-flexx\node-server\node_modules\firebase-admin\lib\utils\error.js:88:28) at new FirebaseAppError (C:\Users\Business\PhpstormProjects\gantt-flexx\node-server\node_modules\firebase-admin\lib\utils\error.js:122:28) at FirebaseNamespaceInternals.initializeApp (C:\Users\Business\PhpstormProjects\gantt-flexx\node-server\node_modules\firebase-admin\lib\firebase-namespace.js:68:23) at FirebaseNamespace.initializeApp (C:\Users\Business\PhpstormProjects\gantt-flexx\node-server\node_modules\firebase-admin\lib\firebase-namespace.js:392:30) at Proxy. (C:\Users\Business\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:338:51) at C:\Users\Business\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:641:30 at Generator.next () at fulfilled (C:\Users\Business\AppData\Roaming\npm\node_modules\firebase-tools\lib\emulator\functionsEmulatorRuntime.js:4:58) at process._tickCallback (internal/process/next_tick.js:68:7) ! Your function was killed because it raised an unhandled error.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:18 (4 by maintainers)

github_iconTop GitHub Comments

112reactions
markgohocommented, Apr 2, 2021

here’s what I’m doing:

const admin = require('firebase-admin');

if (admin.apps.length === 0) {
  admin.initializeApp();
}
9reactions
renatosantosticommented, Mar 26, 2021

Tente: !firebaseAdmin.apps.length ? firebaseAdmin.initializeApp() : firebaseAdmin.app(); ou try{ firebaseAdmin.initializeApp() } catch(err){ firebaseAdmin.app() }

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - initializeApp when adding firebase to app and to server
The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second ...
Read more >
error: the default firebase app already exists. this means you ...
This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need...
Read more >
Fixing The 'FirebaseApp name already exists!' Error - Medium
As there is no longer a default firebase call, you'll have to import getApps() first to check for any existing Firebase apps.
Read more >
Keep getting "Firebase App named '[DEFAULT]' already exists ...
You should only initialize it once for the app (assuming you're only connecting to one project). It will then be initialized and available...
Read more >
Error the Default Firebase App Already Exists (Node.js)
This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found