firebase / firebase-tools

The Firebase Command Line Tools
MIT License
4.02k stars 939 forks source link

The default Firebase app does not exist. Make sure you call initializeApp() #5980

Open christhompsongoogle opened 1 year ago

christhompsongoogle commented 1 year ago

Discussed in https://github.com/firebase/firebase-tools/discussions/5910

From Geoffrey-Pliez:

the script no longer passed in the firebase-frameworks/firebase-aware.

Here is the minimal source code to reproduce the problem.

When I test locally with the prod_preview command (set FIREBASE_FRAMEWORKS_BUILD_TARGET=production&& npm run emulators), it works correctly and the code passes well in firebase-aware.

When I deploy on the server (firebase hosting) via the firebase deploy command, it no longer works and get the message [ssr] Error: The default Firebase app does not exist. Make sure you call initializeApp() before using any of the Firebase services. and guess that on the server the firebase-aware script is no longer called.

I'm not sure but I think it's since upgrading from 11.30.0 to 12.0.0

steebchen commented 1 year ago

Potentially also running into this with next-firebase-auth it seems (although it happens when upgrading firebase and not firebase-tools)

DellaBitta commented 1 year ago

@Geoffrey-Pliez,

I'm from the Firebase Web SDK team. I was looking a bit into this issue and I'm trying to reproduce this here to see if this was a Firebase Web Frameworks issue or a Firebase Admin SDK issue.

I haven't been able to get this to work with the latest or older versions of the firebase-admin and firebase-tools SDKs. All versions seem to have problems at the build step (the same error as you reported) which is fixed by initializing the firebase-admin SDK as described here.

I should note that I was able to reproduce this issue without Firebase Web Framework support (from firebase-tools) simply by creating an Astro app from scratch. I went through these steps:

  1. npm create astro@latest and walking through the configuration steps.
  2. npm install firestore-admin.
  3. Updating the code at the top of index.astro from:
---
import Layout from '../layouts/Layout.astro';
import Card from '../components/Card.astro';
---

to:

---
import Layout from '../layouts/Layout.astro';
import Card from '../components/Card.astro';

import { getFirestore } from 'firebase-admin/firestore'
const firestore = getFirestore()
---
  1. npm run build.

This produces the same Error: The default Firebase app does not exist that you reported. The error goes away if I add:

import { initializeApp } from 'firebase-admin/app'
const app = initializeApp();

Could you step me how you've setup your environment past these steps to make this work with the older admin / firebase-tools versions without calling initializeApp first, as is the case in your provided code?

Thanks very much.

Geoffrey-Pliez commented 1 year ago

@DellaBitta

Sorry, I don't remember very well because currently I work around the problem by initializing firebase-admin in Astro middleware.

As soon as I have time, I will try to investigate why it worked before for me.

aayman997 commented 10 months ago

Any updates on this issue? I'm facing the same error but in Next.js The default Firebase app does not exist. Make sure you call initializeApp() When I try to initializeApp() inside the API route or middleware?

DellaBitta commented 10 months ago

Hi @aayman997,

We're still seeking a reproducible test case beyond the originally reported one, which I had already detailed how I was able to update the code to make it work. If you have a different issue and setup then please create a new issue. Thanks!

YusufTezel commented 7 months ago

Hi Sorry, but it doesn't work. Can you please be more specific about how to work around this issue which still persists?

JoaquinFF commented 2 months ago

Hello, are there any updates on how to solve this issue? I'm using nextjs and seems to work sometimes and sometimes it doesn't