1

EDIT (ANSWER): There were two issues with my code. One was that I was passing "http://localhost" instead of just "localhost" to the connectFirestoreEmulator line. The second was that I needed to check db._settingsFrozen before running the emulator line in order to prevent it from being double-initialized. I believe this has something to do with how Nuxt runs its code on the server side vs client side: I think Nuxt shares context/state across sessions on the server side, leading to the emulator line getting run more than once.

I have a Nuxt 2 app that I've connected to Firebase production successfully for a while now. I'm trying to set it up now to use the emulator instead of production when in local environment (indicated by an env variable), but I'm getting an error with the Firebase module specifically. There doesn't seem to be any issues using the auth emulator (which I've also set up). Code (plugins/firebase.js):

const app = initializeApp(firebaseConfig);

const auth = getAuth(app);
const db = getFirestore(app);
export const services = { auth, db };

export default (context) => {
    if (context.env.appEnv === 'dev') {
        console.log('dev', parseInt(context.env.firestorePort))

        connectFirestoreEmulator(db, 'http://localhost', parseInt(context.env.firestorePort));
        connectAuthEmulator(auth, `http://localhost:${context.env.authPort}`);
    }
}

I have narrowed it down to the "connectFirestoreEmulator" line which when commented out, there are no errors. When it's included, this is the error I get:

Firestore has already been started and its settings can no longer be changed. You can only modify settings before calling any other methods on a Firestore object.

Finally, when I set the plugin to run on client-side only, it no longer errors out which is odd. I'll probably just keep working with this client-only for now, but I would like to be able to run the firebase stuff on the server too ideally.

Any ideas/guidance is appreciated. Thanks in advance!

I've Googled the relevant terms and found one other question that was similar to my question ("Firebase Error : Firestore has already been started and its settings can no longer be changed." connecting Firebase v9 with Firestore Emulator). However, because I don't have enough reputation on SO, I can't comment to ask the OP if he ever found out what was happening (which I would normally do before asking my own question, but I'm not being given a choice here).

I also even looked at the nuxt-firebase npm package's source code (https://github.com/nuxt-community/firebase-module) to see how they may have set up the emulator, but their plugin/code is written so differently from mine that it was unhelpful.

Millan Singh
  • 61
  • 1
  • 6
  • Does omitting the http:// scheme help? the docs show only `connectFirestoreEmulator(db, 'localhost', 8080);` – Chris Dec 06 '22 at 01:47
  • @Chris, that was indeed one of the issues. There were two that I just put up as an edit to the original question for anyone who stumbles on the same issue in the future. I just figured it out today. – Millan Singh Dec 07 '22 at 22:44

0 Answers0