Attempted import error: firebase/app has no default export
Attempted 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.
Fix it fast
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/" src2Match what you see, then jump to its fix
- 1
The 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
You 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
The 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
- 1
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.json2Fix
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 startConfirmed by firebase.google.com
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" src2Fix
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 buildConfirmed by firebase.google.com
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)\(" src2Fix
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)\(" srcConfirmed by firebase.google.com
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Web apps
Firebase Messaging cannot register its default service worker
messaging/failed-service-worker-registration
Firebase Messaging registers a file called firebase-messaging-sw.js from the root of your domain.
3 causes, each with a fixWeb apps
Firestore addDoc throws on a field whose value is undefined
FirebaseError: Function addDoc() called with invalid data. Unsupported field value
Firestore has no undefined type.
2 causes, each with a fixWeb apps
A Vite single page app shows 404 when you refresh a route on the live site
Page not found (Netlify) on a route that works inside the app
Client side routes like /dashboard are not real files in dist.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fix