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.

How to connect `firebase-admin` to the firebase emulators?

See original GitHub issue

There is a related issue (https://github.com/firebase/firebase-admin-node/issues/575) but it seems that it’s still unresolved.

We would like to connect the admin SDK to firebase emulators when running tests. Our setup code looks like this:

import * as admin from 'firebase-admin'
admin.initializeApp({ credential: ... })
export default admin

Then in every file that needs access to firestore, we use:

import firebase from `./firebase`
firebase.firestore().doSomething()

How can we modify this setup so that is connects to the firebase emulators instead?

This is what we’ve tried so far:

  • Have the setup code return the @firebase/testing namespace depending on the node environment. However this library doesn’t setup a default firebase app so calling firebase.firestore() fails.
  • Have firebase-admin connect to the emulators directly, by providing an explicit databaseURL and firestore URLs, and overriding credential.getAccessToken() to return owner. Now the issue is that the admin’s implementation of firestore expects a service account and throws on the credentials override: https://github.com/firebase/firebase-admin-node/blob/master/src/firestore/firestore.ts#L95-L100
  • Return the firebase app as part of the setup code, so that app.firestore() always works. This requires refactoring the whole codebase to use an app instance instead of a firebase workspace.

Is there a recommended way to connect the admin sdk to emulators?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

37reactions
fouchekeagancommented, Dec 28, 2020

The Admin SDKs support the FIRESTORE_EMULATOR_HOST environment variable. You can simply set it to localhost:8080 to connect directly to the emulator.

Let us know if you have any other questions, but for now I hope you are unblocked.

For those in the future, please note this is an environment variable and not a parameter to the admin SDK. To elaborate this, if you are running a NodeJS backend/script:

const useEmulator = true;

if (useEmulator){
    process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080';
}

admin.initializeApp({ ... });

This will set the environment variable from within code (not recommend for production environments), you can read more in this S.O Article for other ways of setting Environment Variables for NodeJS.

Do not try to do it this way:

admin.initializeApp({
    ...
    FIRESTORE_EMULATOR_HOST: 'localhost:8080'
});

It will have no effect locally, only remotely.

34reactions
schmidt-sebastiancommented, Jan 31, 2020

The Admin SDKs support the FIRESTORE_EMULATOR_HOST environment variable. You can simply set it to localhost:8080 to connect directly to the emulator.

Let us know if you have any other questions, but for now I hope you are unblocked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect your app to the Authentication Emulator - Firebase
In the Emulator Suite UI, click the Authentication tab. Click the Add user button. Follow the user account creation wizard, filling in the...
Read more >
Firebase Admin SDK does not use emulator - Reddit
I'm trying to use Admin SDK in emulator, but instead when initializing it tries to connect to the real project, and because I...
Read more >
How to connect Firebase Admin to Emulator Auth
I want to connect firebase-admin and get a list of users in the emulated Auth instance. Many thanks for any help you can...
Read more >
How to configure Firebase emulators with Next.js?
However, using the firebase-admin library in the backend scenario, you have to provide a private key to connect to the Firebase. It would...
Read more >
Testing and Local Development - Firebase Admin SDK for PHP
Auth Emulator¶ · Firebase Admin SDKs automatically connect to the Authentication emulator when the · With the environment variable set, Firebase Admin SDKs...
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