Home > Net >  React Native firebase is undefined
React Native firebase is undefined

Time:11-03

Hi I keep getting this error when trying to run my react-native app on expo go on my phone

TypeError: undefined is not an object (evaluating '_app.default.apps') and the error shows it pointing to the firebase variable on line 19 in the code below in the if statement where it says firebase:


import firebase from 'firebase/app';
require('firebase/auth');
import Constants from 'expo-constants';


// Initialize Firebase
const firebaseConfig = {
  apiKey: Constants.manifest.extra.apiKey,
  authDomain: Constants.manifest.extra.authDomain,
  projectId: Constants.manifest.extra.projectId,
  storageBucket: Constants.manifest.extra.storageBucket,
  messagingSenderId: Constants.manifest.extra.messagingSenderId,
  appId: Constants.manifest.extra.appId,
  measurementId: Constants.manifest.extra.measurementId
};

let Firebase;

if (firebase.apps.length === 0) {
  Firebase = firebase.initializeApp(firebaseConfig);
}

export default Firebase;

this in turn is causing I believe an Invariant violation as well.

Any help please?

CodePudding user response:

FirebaseSDK changed their API in the new version 9 so now import firebase from “firebase/app” won’t work

You need to use the import { initializeApp } from 'firebase/app'; to initiate your app or import from firebase/compat/app to use the old API

Refer to https://firebase.google.com/docs/web/modular-upgrade for more about the changes and https://firebase.google.com/docs/web/setup for the new API

  • Related