The Problem
Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app) happens wheninitializeApp() runs more than once for the same app instance. It is extremely common in Next.js because hot module reloading and repeated module imports can re-execute your Firebase setup file multiple times during development.
Why It Happens
Firebase only allows one app registered under the name [DEFAULT] at a time. The error fires when:
initializeApp()is called in a module that gets re-imported or re-evaluated, such as during Next.js Fast Refresh- Firebase setup code runs in both a client component and a server component, initializing twice
- A shared config file is imported from multiple places without memoizing the app instance
The Fix
1. Guard initialization with getApps()
Check whether an app already exists before calling initializeApp() again:
import { initializeApp, getApps, getApp } from 'firebase/app';
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
};
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);
export { app };
getApps() returns every initialized app; if the array is empty, it's safe to initialize, otherwise reuse the existing one with getApp().
2. Export a single shared instance
Put this in one file, such as lib/firebase.ts, and import the exported instances everywhere instead of calling initializeApp() again:
// lib/firebase.ts
import { initializeApp, getApps, getApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
import { getAuth } from 'firebase/auth';
const firebaseConfig = {
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
};
const app = getApps().length ? getApp() : initializeApp(firebaseConfig);
export const db = getFirestore(app);
export const auth = getAuth(app);
export default app;
3. Import from the shared file everywhere
import { db, auth } from '@/lib/firebase';
Never call initializeApp() a second time in a component, API route, or server action — always import the shared app, db, and auth exports.
4. Check for duplicate setup across client and server
If you have separate Firebase setup for server components (e.g. Firebase Admin SDK) and client components (Firebase JS SDK), make sure they use different app names or are kept in fully separate modules — the admin SDK's initializeApp and the client SDK's initializeApp do not share the same app registry, but each can still throw this error internally if called twice within its own SDK.
How to Prevent It
- Centralize Firebase initialization in a single module and import from it everywhere
- Always guard with
getApps().lengthbefore callinginitializeApp() - Never call
initializeApp()inside a component body or a function that runs on every render - For the Admin SDK, apply the same guard using
admin.apps.lengthbeforeadmin.initializeApp()
Frequently Asked Questions
Why does this error only show up in development, not production?Next.js Fast Refresh re-executes modules on file changes during development, which is what triggers repeated initializeApp() calls. Production builds don't hot-reload, so the duplicate call rarely happens there.
Yes, use the equivalent guard: const app = admin.apps.length ? admin.app() : admin.initializeApp(config);
Yes, pass a second argument to initializeApp(config, 'secondaryApp') to register additional named apps, then retrieve them with getApp('secondaryApp').
getApps() on every import?
Yes, getApps() is a cheap synchronous lookup against Firebase's internal registry and is the recommended pattern for this exact scenario.
Need Help?
If Firebase setup issues keep slowing down your Next.js builds, we can restructure your project's initialization so it just works. Explore our development services or get in touch.
Related Services
Need help building this?
Our team specializes in exactly this kind of work. Get a free quote and honest assessment within 24 hours.
Start a Project