firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

The default Firebase app already exists

Open Applicafroguy opened this issue 4 years ago • 10 comments

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'

Originally posted by @Applicafroguy in https://github.com/firebase/firebase-tools/issues/1532#issuecomment-1125554114

Applicafroguy avatar May 13 '22 00:05 Applicafroguy

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

Glad you found the issue!

bkendall avatar May 13 '22 16:05 bkendall

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'

Originally posted by @Applicafroguy in #1532 (comment)

I've got the exact same error when I was using node-fetch. Did you switch to any alternative e.g. axios or you used any other solution?

Edit: I solved my problem by downgrading the node-fetch from version 3 to version 2. It turns out that I imported the node-fetch with require() for the v3 node-fetch, while v3 is a ESM-only module.

npm install node-fetch@2

Kiaala6 avatar Jun 07 '22 11:06 Kiaala6

I have the same issue.

I solved my problem by downgrading the node-fetch from version 3 to version 2. It turns out that I imported the node-fetch with require() for the v3 node-fetch, while v3 is a ESM-only module.

It helps me, thanks. But this workaround is not an essential solution, and I am concerned that this Issue is closed. I would like to see this issue addressed to work with the latest node-fetch.

kyoh86 avatar Jul 17 '22 02:07 kyoh86

Any updates on this? Error still is present, I would consider reopening this @Applicafroguy...

polarby avatar Sep 12 '22 06:09 polarby

I am having the same issue. Why is this closed?

nietsmmar avatar Oct 29 '22 20:10 nietsmmar

If it's of any help to anyone, I was facing this same error. Just by trying to rollback different changes I discovered it had something to do with got package:

I was having this error with "got": "^12.5.3" My deploy went back to working when I downgraded to "got": "^11.8.5"

Didn't try exactly which version en between those broke things, and have absolutely no idea why would firebase error on initializeApp() affected by the got package.

conceptualben avatar Dec 17 '22 05:12 conceptualben

I found that commenting out the admin.initializeApp(); call caused the error message to reveal the offending packages.

robflaherty avatar Feb 05 '23 14:02 robflaherty

Since updating firebase-tools to the latest version (^12.2.0), I had to update Node.js as well to version >=16.13.0 || >=18.0.0.

After researching this new error, I landed on this page saying the cause was the node-fetch package. Then by chance, I came across this blog announcing that the Fetch API was also released on Node.js since v17.5. I thought that maybe the fetch function might be conflicting between the built-in one and the one from node-fetch. Hence, I removed the import line import fetch from 'node-fetch' and the error went away. The fetch function still works as intended.

Finally, I was able to uninstall node-fetch from my project completely as it's useless now.

Dara-To avatar May 25 '23 06:05 Dara-To

I found that commenting out the admin.initializeApp(); call caused the error message to reveal the offending packages.

This saved my day. Thx so much ❤️

dennyok avatar Jan 25 '24 11:01 dennyok

In case anyone is trying to import their own package to use with their Firebase Functions and are trying to make it CommonJS-compatible, this post was very helpful: https://stackoverflow.com/a/75348391/12383957

Although apparently there is ES module support for Cloud Functions now? https://medium.com/google-cloud/es-modules-in-cloud-functions-f5be1676c8b5

CameronMcClymont avatar Jan 08 '25 21:01 CameronMcClymont