Skip to content

Connect to firestore emulator with admin SDK requires supplying the project-id #2602

Closed

Description

@ZYinMD

Edit:
Summary of the discussion below:

  1. The firebase admin SDK must init with some project-id, but it doesn't have to be a real one.
  2. Meanwhile, if you also want to visualize the db using the emulator UI, or if you're using the function emulator at the same time, then the emulator must start with the same project-id as the admin SDK. This can be achieved with firebase --project=some_project_id emulators:start. Without the flag, the emulator will use the project id in your .firebaserc, and create a different instance of db running parallel with the admin SDK's instance.

===========

Original issue:

I found that when I have a firestore running on local emulator, and I want to directly access it with a script using the admin SDK, I need to supply the project-id to the admin SDK, which I believe is not the intended behavior.

I use the nodejs firebase-admin package, not sure if the bug is theirs.

[REQUIRED] Environment info

firebase-tools:
8.10.0
npm firebase-admin package
9.1.1

Platform:
Windows

[REQUIRED] Test case

see below

[REQUIRED] Steps to reproduce

  1. have the firestore emulator running
  2. create a test.js locally:
const admin = require('firebase-admin'); 
admin.initializeApp();
admin.firestore().collection('foo').add({ a: 1 });
  1. run it: $ FIRESTORE_EMULATOR_HOST="localhost:8080" node test.js

[REQUIRED] Expected behavior

should see a document created in the emulator firestore UI

[REQUIRED] Actual behavior

Error: Unable to detect a Project Id in the current environment.

To fix this, I need to supply the project id to the admin SDK:

admin.initializeApp({ projectId: 'my-project-id' }); // now works

But I believe it's not the intended behavior.

Activity

  1. markgoho commented on Sep 7, 2020

    @markgoho

    try

    const firebase = require('@firebase/testing');
    
    const MY_PROJECT_ID = 'my-project';
    
    function getAdminFirestore() {
      return firebase.initializeAdminApp({ projectId: MY_PROJECT_ID }).firestore();
    }
  2. ZYinMD commented on Sep 7, 2020

    @ZYinMD
    Author

    Hi @markgoho , thanks, your snippet was the same as mine (at the bottom of my post).

    The issue is not getting it to work, the issue is that project-id shouldn't be required. This code is very likely for the purpose of unit testing, and 1. I may change the project id with firebase use, and 2. I may not want to commit my project-id into source control for open source projects, which is why .firebaserc is a separate file from firebase.json.

  3. samtstern commented on Sep 8, 2020

    @samtstern
    Contributor

    @ZYinMD this is the intended behavior, without some project ID (does not have to be a real one!) the Firestore Admin SDK can't construct the right calls to the Firestore API, or in this case the Firestore Emulator.

    If you want to set this without modifying source code, use the GCLOUD_PROJECT environment variable which is respected by all Cloud SDKs.

  4. ZYinMD commented on Sep 9, 2020

    @ZYinMD
    Author

    Hi @samtstern , thanks! Should it be mentioned in the doc here?

    Also, about "project ID doesn't need to be a real one" that you mentioned: I tried to set it to 'foo', and indeed, it still works, which blew my mind. Should it also be mentioned in the doc?

  5. samtstern commented on Sep 10, 2020

    @samtstern
    Contributor

    @ZYinMD I will update the docs, thanks!

  6. samtstern commented on Sep 10, 2020

    @samtstern
    Contributor

    @ZYinMD just FYI if you're using the Firestore and Functions emulators together make sure they have the same project ID! Otherwise triggers won't fire, etc.

  7. ZYinMD commented on Sep 11, 2020

    @ZYinMD
    Author

    @samtstern to clarify: the emulator reads the real project-id from .firebaserc, and the admin-sdk running with the functions is also part of the emulator, which also reads the same project-id, which has no issues. However, the admin-sdk in the test suites mentioned in the OP is manually given a project-id, and you're saying this one should also be the same one, which is the real id, correct?

  8. samtstern commented on Sep 15, 2020

    @samtstern
    Contributor

    @ZYinMD sorry for the slow response but yes that's exactly it.

  9. vajahath commented on Oct 8, 2020

    @vajahath

    @samtstern If you could also add a single line code to that documentation, that would be very nice. like:

    admin.initializeApp({ projectId: 'my-project-id' });
  10. samtstern commented on Oct 9, 2020

    @samtstern
    Contributor

    @vajahath will do!

  11. ZYinMD commented on Oct 14, 2020

    @ZYinMD
    Author

    Hi @samtstern, I see that you have updated the doc since the discussion above.

    I just found that using a fake project-id has more problems than I thought - the documents inserted into the db with a fake project-id won't show up in the emulator UI, which means the setup of a fake-project id + admin-sdk + emulator for prototyping is pointless.

    You might want to make it more clear in the doc, the way it's written now is a little misleading because it advertises something that sounds great, but don't really work.

    It's also worth noting that this setup is still usable in unit testing, because the tests don't involve the emulator UI. However, the test becomes flaky when it involves both directly accessing the db and accessing the db via functions, because the functions will always use the real project-id stored in firebaserc.

  12. samtstern commented on Nov 2, 2020

    @samtstern
    Contributor

    @ZYinMD we're working on showing multiple databases in the Firestore Emulator UI, you can follow that here:
    firebase/firebase-tools-ui#281

  13. JamieDixon commented on Jan 20, 2021

    @JamieDixon

    @ZYinMD Your comment here lead me to realise that the project-id relates to the project being used by firebase use. I was completely unaware that this was the case and was struggling to understand how the project-id had any relevance given that the docs say you can use anything you want. Thank you!

  14. MchlG commented on Jan 27, 2021

    @MchlG

    So is there no way to view data in the Firestore emulator UI while using a fake project id? Right now the data only displays in the UI when I use the project ID that matches the project which I set via firebase use which means I have to use my real project id...

  15. samtstern commented on Jan 27, 2021

    @samtstern
    Contributor
  16. thMthMnky commented on Mar 10, 2021

    @thMthMnky

    With the following environment variables:

    GCLOUD_PROJECT="test-my-project" 
    
    FIREBASE_AUTH_EMULATOR_HOST="localhost:9099",
    

    The following initialization seemed to allow the admin SDK to use the firestore emulator but it only allowed partial use of the auth emulator (calls to admin.auth().listUsers() returned an empty array despite there being at least one emulated user):

    import * as admin from 'firebase-admin'
    
    admin.initializeApp({ projectId: process.env.GCLOUD_PROJECT});
    
    const Firestore = admin.firestore();
    
    Firestore.settings({
          host: "localhost:8080",
          ssl: false
    });
    

    Currently using:
    firebase-tools: v9.4.0
    npm firebase-admin package v9.5.0

  17. okric commented on Jun 20, 2021

    @okric

    I faced a similar problem. I was using two different projects, one for development another for production.
    There are many places where the project ID is used, and a mismatch can yield weird bugs that can be hard to troubleshoot:

    • .firebaserc on your project directory
    • the admin.initializeApp() call
    • the url used to call the functions (HTTP 404 when wrong)
    • the RTDB name, if kept the default
    • the service Account json file
    • the google-services.json file
      More?
  18. samtstern commented on Jun 21, 2021

    @samtstern
    Contributor

    @RicardoAratani we are working on a plan to make it much more obvious when you have a project ID mismatch and also make it easier to set things up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions