firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.78k stars 876 forks source link

`IDBIndex is not defined` runtime exception in Node.js when importing ESM #6042

Closed jamesdaniels closed 2 years ago

jamesdaniels commented 2 years ago

[REQUIRED] Describe your environment

Including analytics, remote config, performance monitoring, FCM, or AppCheck in your server bundle while targeting ESM leads to a runtime failure IDBIndex is not defined; even if the code is not executed or guarded by platform checks / isSupported.

This is a problem as frameworks that build for the server may target ESM in Node.js. Angular for instance started doing this in 13.1+ by default.

This significantly impacts AngularFire, since we check isSupported from Analytics, RC, and Messaging in the main bundle due to how Angular's Dependency Injection operates. Apps using our library can't upgrade to the latest versions of Angular.

The root cause looks to be that the older version of the idb package (dependency of @firebase/installations) is not using the exports field in its package.json; it expects Node.js to load the CJS. Their ESM blindly expects IndexDB classes to exist. As I understand we can't simply upgrade idb (assuming they've fixed this in a recent version) since they dropped support for Internet Explorer in its entirety.

As discussed this may turn into a whack a mole situation, as this exception may just be the first of many encountered. Other packages may be setup incorrectly to support modules in Node.js. I will be exploring further.

Context

https://github.com/angular/angularfire/issues/3113 https://github.com/angular/angularfire/issues/3102 https://github.com/angular/universal/issues/2475 https://github.com/nrwl/nx/issues/8767

puf commented 2 years ago

There have also been four (so far) questions with this error on Stack Overflow in the past few days: https://stackoverflow.com/search?tab=newest&q=%5bfirebase%5d%20IDBIndex. Can we confirm whether this is an issue in the latest SDK release?

arendjantetteroo commented 2 years ago

@puf this started happening for us when we upgraded from firebase 9.6.7 to 9.6.8 today. For now we "fixed" it by setting 9.6.7 as our version to use. We build in a gatsby environment, so with SSR as well.

jamesdaniels commented 2 years ago

@arendjantetteroo might be a different root cause if downgrading fixed, what firebase modules are you importing?

arendjantetteroo commented 2 years ago

@jamesdaniels We currently only use the auth module

actuallymentor commented 2 years ago

Can confirm that I'm also running into this issue. The known source of this error is importing analytics on a non-web environment, but it appears to now be triggered upon import of any module.

I'm also running into this issue after downgrading, so either I am having caching issues or something changed on the backend of firebase causing the running of IDBIndex reliant code inappropriately.

jamesdaniels commented 2 years ago

Thanks for chiming in. We've confirmed that this issue (and the same root cause) has spread to other products in recent releases, a fix is in play #6061.

CarsoteCosmin commented 2 years ago

any updates on this?

rejhgadellaa commented 2 years ago

@CarsoteCosmin it's live in 9.6.9: https://firebase.google.com/support/release-notes/js#version_969_-_march_17_2022

lifeofgurpreet commented 2 years ago

Great news!

ReeceKenney commented 2 years ago

Yes, I tried this morning and it's all working again. Thanks!

ProSugu commented 2 years ago

Got another one after the above issue https://github.com/angular/angularfire/issues/3181

yharaskrik commented 2 years ago

I am also experiencing https://github.com/angular/angularfire/issues/3181

If anyone has a workaround I would love to know! We had to disable our SSR for the time being.