How to connect `firebase-admin` to the firebase emulators?
See original GitHub issueThere 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 callingfirebase.firestore()
fails. - Have
firebase-admin
connect to the emulators directly, by providing an explicitdatabaseURL
and firestore URLs, and overridingcredential.getAccessToken()
to returnowner
. 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:
- Created 4 years ago
- Reactions:7
- Comments:12 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
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:
It will have no effect locally, only remotely.
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.