question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
thMthMnkycommented, Mar 10, 2021

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

3reactions
samtsterncommented, Jun 21, 2021

@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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect your app to the Cloud Firestore Emulator - Firebase
If you want your Admin SDK code to connect to a shared emulator running in another environment, you will need to specify the...
Read more >
connecting to firestore emulator from java admin sdk
The Firebase Admin SDKs automatically connect to the Cloud Firestore emulator when the FIRESTORE_EMULATOR_HOST environment variable is set:.
Read more >
Testing apps locally with the emulator | Cloud Pub/Sub ...
The string does not need to represent a real Google Cloud project because the Pub/Sub emulator runs locally. gcloud beta emulators pubsub start...
Read more >
Building a REST API with Firebase cloud functions, TypeScript ...
However, to be able to use Firebase functions, you'll need to upgrade your billing plan to the Blaze Pay as you go. We...
Read more >
firebase instance id not found | The Search Engine You Control
firebase/firebase-toolsConnect to firestore emulator with admin SDK requires supplying ... To fix this, I need to supply the project id to the admin...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found