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

Most upvoted comments

import { initializeApp} from 'firebase-admin'
initializeApp()

Now gives me this error (used to work). While the following now works

import * as admin from 'firebase-admin'
admin.initializeApp()

Came across the same error while using it together with TypeScript:

import * as admin from 'firebase-admin';

The key to solve the issue was to set the allowSyntheticDefaultImports in the tsconfig.json to true and then to import it as default import:

import admin from 'firebase-admin';

Just in case anyone else comes across this issue, make sure you import looks this

import { initializeApp } from 'firebase-admin/app';

not this

import { initializeApp } from 'firebase-admin';

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.

Cloud functions cold start time is already bad enough, small imports are essential here.

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 entire admin 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 and FirebaseApp classes (see #1432). These changes will eventually make it possible for you to do things like this:

import { initializeApp } from 'firebase-admin/app';

But in the meantime the Admin SDK only (officially) supports the following imports:

// CJS
const admin = require('firebase-admin');

// TypeScript
import * as admin from 'firebase-admin';

For any other import style you may want to try YMMV.

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:

import * as functions from "firebase-functions";
import * as firebase from "firebase-admin";

firebase.initializeApp();

export default async (
  data: any,
  context: functions.https.CallableContext,
) => {
  console.log('Hello');
};

This doesn’t:

import * as functions from "firebase-functions";
import { initializeApp } from "firebase-admin";

initializeApp();

export default async (
  data: any,
  context: functions.https.CallableContext,
) => {
  console.log('Hello');
};

Error message:

Unhandled error TypeError: Cannot read property 'INTERNAL' of undefined
    at FirebaseNamespace.initializeApp (/workspace/node_modules/firebase-admin/lib/firebase-namespace.js:387:21)
    at Object.<anonymous> (/workspace/lib/onCall/test.js:4:36)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at /workspace/lib/index.js:708:48
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

do

import { initializeApp } from 'firebase-admin/app';

don’t

import { initializeApp } from 'firebase-admin';

tsconfig.json

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    // .
    // .
    // . 
    // Other options
}

This issue still exists using adminsdk 11.10.1

Came across the same error while using it together with TypeScript:

import * as admin from 'firebase-admin';

The key to solve the issue was to set the allowSyntheticDefaultImports in the tsconfig.json to true and then to import it as default import:

import admin from 'firebase-admin';

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.

@josh18 It seems that firestore isn’t part of firebase-admin/app and it exists only firebase-admin I’m still facing this issue of Cannot read property 'INTERNAL' of undefined at FirebaseNamespace.initializeApp

I’m using "firebase-admin": "^10.0.0" With NestJs

import { initializeApp, credential, ServiceAccount } from 'firebase-admin';
const serviceAccount = require('google-service.json');
export const firestore = initializeApp({
    credential: credential.cert(serviceAccount as ServiceAccount ),
}).firestore();

@chaosLegacy , import from ‘firebase-admin/app’ instead of ‘firebase-admin’.

@chaosLegacy to get firestore I use this

import { getFirestore } from 'firebase-admin/firestore';

const firestore = getFirestore();

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

@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.

I can confirm this. Tried on several projects with the latest typescript and it works.