Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Attempted import error: firebase/app has no default export

ErrorAttempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').

Short answer

Since the modular SDK (version 9), firebase/app no longer exports one firebase object. It exports functions such as initializeApp. Old code that does import firebase from 'firebase/app' breaks. Either import from firebase/compat/app to keep the old style, or rewrite the calls with named imports.

  • Applies to Firebase JS SDK 12 and 13, React, webpack or Vite
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find every place the code still uses the old default import. Each line listed is one file to change.

    grep -rn "from ['\"]firebase/" src
  2. 2Match what you see, then jump to its fix

    1. 1The import line is import firebase from 'firebase/app', followed by firebase.auth() or firebase.firestore() callsNamespaced (v8-style) code runs on the modular SDKFix
    2. 2You are writing new code, or following a current Firebase guide, and copied the old import from an older tutorialYou want the modular API, so initializeApp must be a named importFix
    3. 3The app is initialized with the modular initializeApp, but other code still calls firebase.auth() or firebase.firestore()Other Firebase products are still called through the old namespaceFix

What does the error look like?

  • Attempted import error: 'firebase/app' does not contain a default export (imported as 'firebase').

Cause 1 of 3

Namespaced (v8-style) code runs on the modular SDK

1Check

Look at the Firebase version in package.json. If it is higher than 8 and the code still calls firebase.something(), the code is written for the old namespaced API.

grep -n '"firebase"' package.json

2Fix

Switch the imports to the compat packages. They are API compatible with the namespaced code, so nothing else has to change on the first day.

import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';

3Verify

Restart the dev server. The import error is gone and the existing firebase.auth() calls still work.

npm start

Confirmed by firebase.google.com

Did this fix it?

Cause 2 of 3

You want the modular API, so initializeApp must be a named import

1Check

In the modular SDK, firebase/app exports individual functions. Search for the old initialize call.

grep -rn "firebase.initializeApp" src

2Fix

Import initializeApp by name and keep the returned app object. Remove the compat imports once the whole codebase is moved.

import { initializeApp } from 'firebase/app';

const firebaseApp = initializeApp({ /* your config */ });

3Verify

The build compiles without the import error.

npm run build

Confirmed by firebase.google.com

Did this fix it?

Cause 3 of 3

Other Firebase products are still called through the old namespace

1Check

After moving to the modular initializeApp, the product services are no longer attached to a firebase object. Look for leftover firebase.* calls.

grep -rnE "firebase\.(auth|firestore|storage|messaging)\(" src

2Fix

Pass the app to the modular service functions instead.

import { getAuth } from 'firebase/auth';
import { getFirestore } from 'firebase/firestore';

const auth = getAuth(firebaseApp);
const db = getFirestore(firebaseApp);

3Verify

Run the app and sign in once; the leftover namespace calls are gone from the grep output.

grep -rnE "firebase\.(auth|firestore)\(" src

Confirmed by firebase.google.com

Did this fix it?

How do I stop it coming back?

  • Follow the current Firebase web guides, not an older tutorial, when you copy setup code.
  • Do not mix compat calls and modular calls on one app unless you are in the middle of an upgrade.
  • Pin the firebase version in package.json and read the release notes before a major upgrade.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.