Skip to content

TypeError: Cannot read property 'INTERNAL' of undefined at FirebaseNamespace.initializeApp #593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
liamdanielduffy opened this issue Jul 16, 2019 · 21 comments

Comments

@liamdanielduffy
Copy link

liamdanielduffy commented Jul 16, 2019

[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?

@google-oss-bot
Copy link

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@liamdanielduffy
Copy link
Author

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

@ofhouse
Copy link

ofhouse commented Apr 28, 2020

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';

@hiranya911
Copy link
Contributor

@ofhouse that sounds like some other problem. We have the following TypeScript integration test which uses the import * as admin from... import: https://github.com/firebase/firebase-admin-node/tree/master/test/integration/typescript

Can you provide a minimal repro of the problem?

@Samuel-Therrien-Beslogic
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()

@Robula
Copy link

Robula commented Sep 10, 2021

Why was this closed? This is still an issue in 9.11.1.
Thanks

@astefer
Copy link

astefer commented Sep 10, 2021

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)

@astefer
Copy link

astefer commented Sep 10, 2021

It's typescript. 4.3.5 works, 4.4.2 doesn't. Hope that helps @hiranya911

@hiranya911
Copy link
Contributor

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.

@larssn
Copy link

larssn commented Sep 13, 2021

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

@radulle
Copy link

radulle commented Oct 1, 2021

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

@josh18
Copy link

josh18 commented Oct 31, 2021

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.

@chaosLegacy
Copy link

chaosLegacy commented Nov 19, 2021

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

@josh18
Copy link

josh18 commented Nov 19, 2021

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

@RoyBkker
Copy link

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

@alexisrougnant
Copy link

I face this issue on my local environment. But everything is working well on a docker environment with the same project (I completely deleted cache and images to prevent any conflict with previous versions).

Both environments share the same version for node (14.18.2), firebase-admin (10.0.1) and typescript (4.5.4). They also share the same typescript config file.

How can this be different between those two environments? Any idea?

@luiswalderdorff
Copy link

For me the problem seemed to be in the package-lock.json. After deleting it (and also the node_modules folder) and running npm i it worked again.

@fishsticks89
Copy link

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

@mdwekat
Copy link

mdwekat commented Sep 3, 2023

do

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

don't

import { initializeApp } from 'firebase-admin';

tsconfig.json

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

@EchoEllet
Copy link

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

Is there an explanation for this fix?

@benedictchen
Copy link

Wow, this makes no sense...

@firebase firebase locked as resolved and limited conversation to collaborators May 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests