Skip to content

Firebase.initializeApp throwing default Firebase app already exists errors in Next.js apps. #2111

@jstncno

Description

@jstncno

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();

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions