[6.3.4] Firebase: Firebase service named ‘database’ already registered (app/duplicate-service)

I’m trying to run the Firebase shell however I get the error:

FirebaseError: Firebase: Firebase service named ‘database’ already registered (app/duplicate-service).

My Firebase dependencies:

"firebase": "^6.3.4",
"firebase-admin": "^8.3.0",
"firebase-functions": "^3.0.2"

I initialize the Firebase in my index.js like:

admin.initializeApp(appConfig);
firebase.initializeApp(appConfig);
const rtdb = admin.database(admin.app());
const firestore = firebase.firestore(admin.app());

Possibly the same issue as, #2040, but was not able to resolve the issues by following those steps.

2 thoughts on “[6.3.4] Firebase: Firebase service named ‘database’ already registered (app/duplicate-service)

  1. I just went through my entire codebase and carefully made sure that I’m at the latest versions of all my google dependencies.

    I fixed every single one of my imports to only import exactly what was needed. No more * as admin or * as firebase. Also no more import admin from 'firebase-admin'

    All my imports look like this now:

    import {https} from 'firebase-functions';
    import {initializeApp as adminInitializeApp, credential, ServiceAccount} from 'firebase-admin'
    import {initializeApp as firebaseInitializeApp} from "firebase";
    

    I’m referencing database from my initialized app:

    const FIREBASE_CONFIG = process.env.FIREBASE_CONFIG || {} as any;
    const adminConfig = JSON.parse(FIREBASE_CONFIG);
    adminConfig.credential = credential.cert(serviceAccount as ServiceAccount);
    const firebaseClientApp = firebaseInitializeApp(clientAccount);
    const firebaseAdminApp = adminInitializeApp(adminConfig);
    

    firebaseAdminApp.database()...

    I’m still getting this error…

    >  FirebaseError: Firebase: Firebase service named 'database' already registered (app/duplicate-service).
    >      at Object.registerService (/functions/node_modules/@firebase/app/dist/index.node.cjs.js:375:33)
    >      at registerDatabase (/functions/node_modules/@firebase/database/dist/index.node.cjs.js:15248:39)
    >      at Object.<anonymous> (/functions/node_modules/@firebase/database/dist/index.node.cjs.js:15271:5)
    >      at Module._compile (internal/modules/cjs/loader.js:776:30)
    >      at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    >      at Module.load (internal/modules/cjs/loader.js:643:32)
    >      at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    >      at Module.require (internal/modules/cjs/loader.js:683:19)
    >      at require (internal/modules/cjs/helpers.js:16:16)
    >      at DatabaseService.getDatabase (/functions/node_modules/firebase-admin/lib/database/database.js:64:24) {
    >    code: 'app/duplicate-service',
    >    name: 'FirebaseError',
    >    appName: 'database'
    >  }
    

    There is no way to use both Admin SDK and Firebase JS SDK in firebase cloud functions with this error.

  2. Changing const firebase = require('firebase') to const firebase = require('firebase/app') allows me to test and deploy my firebase functions. However, the firebase shell gets stuck when I try to locally emulate my functions when I run firebase functions:shell.