firebase-admin-node: TypeError: Cannot read property 'INTERNAL' of undefined at FirebaseNamespace.initializeApp
[REQUIRED] Step 2: Describe your environment
- Operating System version: macOS 10.14.4 (18E226)
- Firebase SDK version: 8.2.0
- Library version: unclear what this means
- Firebase Product: initialization of admin app
[REQUIRED] Step 3: Describe the problem
Steps to reproduce:
Clone the following repository: https://github.com/liamdanielduffy/firebase-admin-bug-repro
Run npm install
or yarn
in the repository root.
Run npm run dev
or yarn dev
to start the server.
Visit localhost:3000/api/graphql
.
See the following message printed in the server logs:
TypeError: Cannot read property 'INTERNAL' of undefined
at FirebaseNamespace.initializeApp (.../firebase-admin-bug-repro/node_modules/firebase-admin/lib/firebase-namespace.js:392:21)
...(rest of stack trace)
Further context
The firebase admin SDK is being initialized in libraries/firebase/initializeAdminApp
.
The service account key is present in the repository, and is exported from libraries/firebase/getKey
.
On each network request to localhost:3000/api/graphql
, the function defined in libraries/apollo/getContext
is called. This function attempts to pass the firebase app as part of the ‘context’ for all the graphql resolvers defined in libraries/nexus/schema
.
I’m not sure why the error is being printed above, but the stack trace above points to this line in the firebase-admin
package: firebase-admin/lib/firebase-namespace.js:392
FirebaseNamespace.prototype.initializeApp = function (options, appName) {
return this.INTERNAL.initializeApp(options, appName);
};
this
is undefined, creating the error thrown.
Why would that happen? Am I initializing this in the wrong way or in the wrong place?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 17
- Comments: 20 (2 by maintainers)
Commits related to this issue
- fix firebase bug with import https://github.com/firebase/firebase-admin-node/issues/593 — committed to collabsoft-net/iapetus by Rawne 3 years ago
- fix: firebase-admin error reffering with https://github.com/firebase/firebase-admin-node/issues/593 — committed to ninjinkun/blog-feedback-app by ninjinkun 3 years ago
Now gives me this error (used to work). While the following now works
Came across the same error while using it together with TypeScript:
The key to solve the issue was to set the
allowSyntheticDefaultImports
in thetsconfig.json
totrue
and then to import it as default import:Just in case anyone else comes across this issue, make sure you import looks this
not this
If it is like ^ Typescript won’t throw an error but firebase will throw an error at runtime.
We don’t support destructured imports in the Admin SDK. That’s why you won’t find that style of imports in any of our existing examples (see https://firebase.google.com/docs/admin/setup#add-sdk). If it had worked in the past then you’ve been lucky.
You won’t get any import size benefits by importing the SDK this way – at least not with our current implementation. Functions like
initializeApp()
are actually implemented as methods of a class, so regardless of how you import you always end up loading the entireadmin
namespace. However, there is some clever lazy loading that’s happening in the SDK, which prevents some of the heavy dependencies like Firestore and Storage from being loaded unless requested by the developer.To get any meaningful import size benefits first we need to expose proper ES6 module entry points (see #1230). Then on top of that we will need to further refactor our implementation, and split the monolithic implementations of
FirebaseNamespace
andFirebaseApp
classes (see #1432). These changes will eventually make it possible for you to do things like this:But in the meantime the Admin SDK only (officially) supports the following imports:
For any other import style you may want to try YMMV.
resolved through the steps described in this spectrum thread: https://spectrum.chat/firebase/general/typeerror-cannot-read-property-internal-of-undefined-at-firebasenamespace-initializeapp~a839b2c8-2349-4bf9-9b21-b4163a916119
Why was this closed? This is still an issue in 9.11.1. Thanks
The workaround @Samuel-Beslogic provided prevents the error but it’s not really a solution. I don’t want to import the whole package when I only need firestore for example. Cloud functions cold start time is already bad enough, small imports are essential here.
Also I don’t have any idea how this even happened. I am on the exact same version of firebase-admin, firebase-functions and firebase-tools that I’ve been using the past few weeks and didn’t change my code in any way but as of today I run into this issue with all functions I (re)deploy. Could this be caused by the environment (firebase cloud functions)?
Versions tested:
firebase-tools: 9.16.5, 9.16.6, 9.18.0 firebase-admin: 9.11.0, 9.11.1 firebase-functions: 3.15.5 node: 14
This works:
This doesn’t:
Error message:
do
don’t
tsconfig.json
This issue still exists using adminsdk 11.10.1
this still works though
@hiranya911 Informative, thanks.
I think you mainly need to
import * as admin from 'firebase-admin'
in your index file. Everywhere else we use destructured imports, from firebase-admin. It works without any issues what so ever, and has been running that way for months on end.@chaosLegacy , import from ‘firebase-admin/app’ instead of ‘firebase-admin’.
@chaosLegacy to get firestore I use this
after initializing the app. See if that works for you.
It’s typescript. 4.3.5 works, 4.4.2 doesn’t. Hope that helps @hiranya911
I can confirm this. Tried on several projects with the latest typescript and it works.