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

[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.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 8
  • Comments: 19 (4 by maintainers)

Most upvoted comments

here’s what I’m doing:

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

if (admin.apps.length === 0) {
  admin.initializeApp();
}

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

Hi. Using 9.1.0 version and still the same

Yes, 7.2.0 works. I was a bug with 7.1.0 then Thanks

Tolotra Samuel Randriakotonjanahary

https://tolotra.com

On Wed, Jul 24, 2019 at 8:47 PM Sam Stern notifications@github.com wrote:

@tolotrasamuel https://github.com/tolotrasamuel we just released firebase-tools version 7.2.0 about 5 minutes ago. That contains the fix for #1479 https://github.com/firebase/firebase-tools/pull/1479 that I was mentioning. Could you upgrade (npm install -g firebase-tools@7.2.0) and see if you still have this issue? If so please provide debug logs again. Thanks for your help and patience!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/firebase-tools/issues/1532?email_source=notifications&email_token=AG7AJXYJ2WLPZW444LLCZI3QBCBQHA5CNFSM4IFPWD6KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD2W523Y#issuecomment-514710895, or mute the thread https://github.com/notifications/unsubscribe-auth/AG7AJX757XRQWNAYM6TWB7LQBCBQHANCNFSM4IFPWD6A .

I guess It depends on how you are using the AdminSDK, what works for me (as I’m using as modules) is this.

const { initializeApp } = require('firebase-admin/app')
const { getAuth } = require('firebase-admin/auth')

if (!app) {
  const app = initializeApp()
  const auth = getAuth(app)
  module.exports = { auth }
}

@markgoho that’s the slam dunk right there ;D thank you.

Doesn’t work for me unfortunately @markgoho admin.apps.length is an [object Object] So now it never gets in the if, and never initializes.

EDIT: I thought that was the problem, but actually I’m getting 1 app on the admin.apps array and that’s why it doesn’t enter the if.

// Init and signIn on Firebase
        console.log(`AAAAAAAAAAAAAAAAAAAA: ${admin.apps.length.toString()}`);
        if (admin.apps.length === 0) {
            admin.initializeApp({
                credential: admin.credential.cert(serviceAccount),
                databaseURL: 'https://****.firebaseio.com',
            });
        }

image

EDIT 2: Nevermind, I was initializing the app inside the exports.myFunction so it always only worked the first time. Hope my “issue” helps someone doing the same mistake.

I have the same issue.

When I deploy firebase deploy --only functions --project staging

Error: Failed to load function definition from source: Failed to generate manifest from function source: 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.

I am using firebase-tools: 10.9.2 , firebase-admin: 10.2.0 and firebase-functions: 3.21.1 I use firebase-functions/v1 and firebase-functions/v2, I only get this error when I export v2 functions

// src/myfunc.ts
import * as f2 from 'firebase-functions/v2'

export const myfunc = f2.https.onRequest(async (request, response) => {
    response.send("Hello")
})
// src/index.ts
import { initializeApp } from 'firebase-admin/app'

initializeApp()

// export functions
export { myfunc } from './myfunc'

I have tried this and I still get the same error

// src/index.ts
let app

if (!app) {
  app = initializeApp()
}

EDIT: Found the issue it was caused by third-party imports

import fetch from 'node-fetch'