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.initializeApp throwing default Firebase app already exists errors in Next.js apps. #2111

Open
jstncno opened this issue Mar 15, 2023 · 11 comments

Comments

@jstncno
Copy link

jstncno commented Mar 15, 2023

Describe your environment

  • Operating System version: macOS 12.6.3
  • Firebase SDK version: "firebase-admin": "^11.5.0"
  • Firebase Product: Admin SDK Firebase.initializeApp
  • Node.js version: v18.14.2
  • NPM version: 9.5.0

Describe the problem

Using Next.js SSR with Firebase consequently calls methods that call Firebase.initializeApp multiple times. This causes the following error to be thrown:

error - FirebaseAppError: 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.

This issue is related to #571 (comment), but the resolution is not helpful and the supplied link (https://firebase.google.com/docs/reference/admin/node/admin.app.App.html#name) results in a 404.

Steps to reproduce:

Create a Next.js app using Server Side Rendering (SSR)

Workaround

I found a workaround by first checking if I've already initialized any apps before calling initializeApp() again:

if (!getApps().length) initializeApp();
@google-oss-bot
Copy link

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@bxclib2
Copy link

bxclib2 commented Jun 17, 2023

I meet the exact same issue. Just use nextjs and it will trigger this issue

@MauricioBorawski
Copy link

I had the same issue with Next 13, in order to fix it I did this workaround:

import { getApps, initializeApp } from "firebase-admin/app";
import { getAuth } from "firebase-admin/auth";

const alreadyCreatedAps = getApps();
const yourFirebaseAdminConfig= {}

const App =
  alreadyCreatedAps.length === 0
    ? initializeApp({yourFirebaseAdminConfig}, "app name")
    : alreadyCreatedAps[0];

Describe your environment:

  • Operating System: Windows 11
  • Node.js version: v18.16.1
  • NPM version: 9.5.1
  • "firebase-admin": "^11.10.0"
  • "next": "13.4.8"

@CSharpTeoMan911
Copy link

CSharpTeoMan911 commented Aug 3, 2023

Thank you for this clever solution

@Kingbonwa1994
Copy link

thanks for this...Next.js 13 has a lot of issues

@mogulist
Copy link

mogulist commented Jan 11, 2024

Thanks to @MauricioBorawski 's solution, I was able to remove the error while HMR.
However, when i run pnpm dev or pnpm build, FirebaseAppError: The default Firebase app does not exist. happened.
With some workaround, I found following code snippet works

const yourFirebaseAdminConfig= {};

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

Describe your environment:

  • Operating System: MacOS 14.1.1
  • Node.js version: v18.18.2
  • PNPM version: 8.11.0
  • "firebase-admin": "^12.0.0"
  • "next": "14.0.4"

@jessejamesrich
Copy link

I'm not sure this solution works, as far as I can tell. It will work on the dev environment, but once deployed Firebase will complain that the app needs to be initialized first. I think that conditional causes problems. By extension, I've had to make code changes to deploy the app.

@marcellintacite
Copy link

marcellintacite commented Jan 30, 2024

I'm not sure this solution works, as far as I can tell. It will work on the dev environment, but once deployed Firebase will complain that the app needs to be initialized first. I think that conditional causes problems. By extension, I've had to make code changes to deploy the app.

@jessejamesrich How did you do for production? i am getting this error after deployment : FirebaseAppError: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services.

@jessejamesrich
Copy link

jessejamesrich commented Jan 30, 2024

On dev I have to use the conditional to check if app exists (!admin.app.length) to make sure admin.initializeApp doesn't run again, but I have to comment out the conditional before deploying as Firebase seems to fail if (I assume) the first process run isn't admin initialization.

This is the best I can figure. It works, but I means you have to make changes to the code before deploying and then back again to get dev working. Here's what it looks like.

// if (!admin.apps.length) {
admin.initializeApp({
	credential: admin.credential.cert(serviceAccount as admin.ServiceAccount),
});
// }

@rafaelbarross
Copy link

Olá, estou enfrentando o mesmo problema.

Estou usando o cron job da Vercel, eu uso o firestore do firebase e a terefa do cron é deletar dados em determinado horário.

Nisso eu preciso usar o admin SDK do google cloud para conseguir ter acesso ao firestore e excluir os dados.

Porém, em produção quando tento chamar minha função cron job exemplo: https://sitename/api/cron, nada acontece.

ja em desenvolvimento funciona perfeitamente.

Quem encontrar alguma solução poderia me ajudar?

@All-Khwarizmi
Copy link

m produção quando ten

Still having this issue?

What's your logs for the https://sitename/api/cron call ?

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests