Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrade to Firebase JS 8.0.0: Attempted import error: 'app' is not exported from 'firebase/app' (imported as 'firebase')

After upgrading to 8.0.0, I get the following error:

Attempted import error: 'initializeApp' is not exported from 'firebase/app' (imported as 'firebase').

My import looks like this:

import * as firebase from "firebase/app" firebase.initializeApp({ ... }) 

TypeScript also complains:

Property 'initializeApp' does not exist on type 'typeof import("/path/to/my/file")'. ts(2339)

How do I fix this?

like image 267
Doug Stevenson Avatar asked Oct 26 '20 22:10

Doug Stevenson


People also ask

How do I fix attempted import error firebase app does not contain a default export imported as firebase <UNK>?

To Solve Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase') Error If You are using Version 9 then don't forget that things changed a bit for importing firebase Now there is a “compatibility” option so can use the /compat folder in your imports.

How do I import Auth from firebase app?

import firebase from "firebase/app"; import "firebase/auth"; Place a FirebaseAuthProvider component at the top level of your app. ( anywhere as long as it's above the other Auth components ). Then use any of the other components anywhere in your component tree.

Was not found in '@ firebase app possible exports default firebase?

To solve the “export 'default' (imported as 'firebase') was not found in 'firebase/app'” error, simply upgrade imports to v9 compatibility, according to Firebase docs.

Can't resolve firebase app react?

To solve the error "Module not found: Error: Can't resolve 'firebase'", make sure to install the firebase package by opening your terminal in your project's root directory and running the command npm install firebase and restart your development server.


2 Answers

In version 8.0.0, the Firebase SDK had a breaking change in the way it handles exports:

Breaking change: browser fields in package.json files now point to ESM bundles instead of CJS bundles. Users who are using ESM imports must now use the default import instead of a namespace import.

Before 8.0.0

import * as firebase from 'firebase/app' 

After 8.0.0

import firebase from 'firebase/app' 

Code that uses require('firebase/app') or require('firebase') will still work, but in order to get proper typings (for code completion, for example) users should change these require calls to require('firebase/app').default or require('firebase').default. This is because the SDK now uses typings for the ESM bundle, and the different bundles share one typings file.

So, you will have to use the new ESM bundle default export:

import firebase from "firebase/app" firebase.initializeApp({ ... }) 

If you are working with SDK version 9.0, read this question instead:

  • How do I fix a Firebase 9.0 import error? "Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase')."
like image 82
Doug Stevenson Avatar answered Sep 26 '22 17:09

Doug Stevenson


old way to import firebase : import * as firebase from "firebase/app";

New way to import in 8.0.0 version : import firebase from "firebase/app"

eg: the way i did it. Only the first 2 lines are relevant, the other lines are only added as apart of my code but its quite general tbh!

import firebase from "firebase/app" import "firebase/auth"  const firebaseConfig = {   apiKey: XXXX,   authDomain: XXX,   projectId: XXXX,   storageBucket: XXXX,   messagingSenderId: XXXX,   appId: XXXX, }   if (!firebase.apps.length) {   firebase.initializeApp(firebaseConfig) }   export const auth = firebase.auth()  export const googleAuthProvider = new firebase.auth.GoogleAuthProvider() 

replace XXXX by ur data, just being clear :)

like image 28
Rmcr714 Avatar answered Sep 23 '22 17:09

Rmcr714