You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to connect firebase-admin to the firebase emulators? #776
There is a related issue (#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.
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?
Reacted by Ik, Sondre Sørbye, Zoltán Mihályi, Nicolas Sayer, Chris Vouga, ir-fuel, Daniel Rodríguez Rivero, Koki Takahashi, cedvdb, Anthony Bernardo and 2 more
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:
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.
Reacted by Edmond Lai, Azzaz, Andrii Ponamarov(Dresha), Arseniy Banayev, CreativeBuilds, Ik, Benjamin Bruneau, Eero Ruohola, denfrancis, LION_PUNK and 36 more
Reacted by Nathan Johnson, Rashid, Kiril Volskyi and Miguel Tiongson
Reacted by Timo, Nathan Johnson, Justinas Bei, Juanma López, Michael Floering, Rashid, Enrico Carnelos, Clarice Bouwer, Riordan, Kiril Volskyi and 3 more
Reacted by Kamal Eddin, Rashid, indu40, Kiril Volskyi and abolger
Side note, setting process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8085'; only affects Firestore. Admin calls made to firebase auth WILL go to remote NOT emulator.
Also, if you don't know what port to set, check firebase.json & look for Firestore port there.
Reacted by Akila Dananjaya, Dustin J Silk, Ian Greenough, Clarice Bouwer, cedvdb, Guilherme C. Matuella, igneus and Sergey Antopolskiy
Yeah, I'm also looking for how to connect the auth utility of firebase-admin?
I tried the docs way of doing it with FIREBASE_AUTH_EMULATOR_HOST. I get no errors, however when trying to utilize the sdk, I get "Error while making request: getaddrinfo EAI_AGAIN http. Error code: EAI_AGAIN"
ℹ️ If you're using Firebase Cloud Storage rather than Firestore, you may need to set FIREBASE_STORAGE_EMULATOR_HOST rather than FIRESTORE_EMULATOR_HOST.
Reacted by David Lemayian, petem24, Oleksii Karpenko, fredericobraga85, Guilherme C. Matuella and Evgeny Astapov
I managed to get this working after a bit of debugging. As per docs FIREBASE_AUTH_EMULATOR_HOST does work. You should not include the protocol part of the url (http/https) e.g: localhost:9099
Can we connect to the emulator without relying on an environment variable? The reason why I ask is that I'd like to create 2 instances of the Firesbase Admin app: 1 for Cloud Firestore and 1 for Emulator Firestore so that I can copy data between the 2. For example:
const cloudAdmin = admin.initializeApp(
{
credential: admin.credential.cert(cloudServiceAccount),
},
'app-name-1',
);
const cloudFirestore = cloudAdmin.firestore();
const emulatorAdmin = admin.initializeApp(
{
// Connect to Emulator?
},
'app-name-2',
);
const emulatorFirestore = emulatorAdmin.firestore();
(async function () {
// Copy data from Cloud Firestore to Emulator Firestore
const data = await cloudFirestore
.doc('widgets/1')
.get();
await emulatorFirestore
.doc('widgets/1')
.set(data.data());
})();
Reacted by Josip Bojčić, Dominic Scheirlinck, Dino Repac and Ivan Spirkovski
The only way I can connect to both the emulator and cloud Firestore in the same script is to use @firebase/testing for the emulator connection and firebase-admin for the cloud connection.
For anyone else who wants to connect to both the emulator and cloud Firestore at the same time, you can do this. It turns out that process.env.FIREBASE_AUTH_EMULATOR_HOST and process.env.FIRESTORE_EMULATOR_HOST are only referenced when initializing the app.
@cedvdb they are documented in both GCP and Firestore docs. I've personally found GCP docs to be more technical, while the Firestore docs are more often what you are looking for/will use. Below I linked to the Firestore docs.
These are both sub-sections of the larger documentation for the entire Emulator Suite, I recommend reading everything in the section to strengthen your understanding of the whole Emulators Suite - even when you are confident with the technology, since the docs are often updated with new features.
@johnnyoshika: It turns out that process.env.FIREBASE_AUTH_EMULATOR_HOST and process.env.FIRESTORE_EMULATOR_HOST are only referenced when initializing the app.
This is true for Firestore, but not true for Firebase Auth (process.env.FIREBASE_AUTH_EMULATOR_HOST).
Specifically, the AuthResourceUrlBuilder (among other things) calls useEmulator(), which checks process.env.FIREBASE_AUTH_EMULATOR_HOST again (e.g. for deciding the URL format). It does this on many (or all?) of the Firebase Auth methods, not just during app (or auth service) initialization.
This makes it really hard to run an emulated and non-emulated instance of Firebase Auth from this admin SDK side-by-side in the same process (because both instances are looking at the same piece of global state regularly)
Reacted by Viraj Patel, Alexey and Guilherme C. Matuella
Ah very nice I was trying to figure out auth emulator for my backend, this answers it. But one question - do I still have to provide a valid config? I was under the impression the emulator of this was so I didn't have to create a second Firebase app for development..
How can I verify that the emulator is actually being used?
I am sending a JWT token from my web client (it's using the Firebase Auth Emulator) to my backend.
However, whily calling verifyIdToken(), I am getting the following error:
{
errorInfo: {
code: 'auth/invalid-credential',
message: 'Must initialize app with a cert credential or set your Firebase project ID as the GOOGLE_CLOUD_PROJECT environment variable to call verifyIdToken().'
},
codePrefix: 'auth'
}
Does this imply, that firebase-admin is not using the emulator?
Activity
google-oss-bot commentedon Jan 30, 2020
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
schmidt-sebastian commentedon Jan 31, 2020
The Admin SDKs support the FIRESTORE_EMULATOR_HOST environment variable. You can simply set it to
localhost:8080to connect directly to the emulator.Let us know if you have any other questions, but for now I hope you are unblocked.
feat: add method to set emulator host programmatically (#319) (#336)
fouchekeagan commentedon Dec 28, 2020
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.
thMthMnky commentedon Mar 10, 2021
I've left a comment in firebase/firebase-tools#2602 (comment) with an admin SDK setup that seems to be working.
FredGithub commentedon Feb 18, 2022
In my case (Windows) I was using:
But I didn't realize that when running in PowerShell you need:
Leaving this here if it helps anyone!
saif97 commentedon May 17, 2022
Side note, setting
process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8085';only affects Firestore. Admin calls made to firebase auth WILL go to remote NOT emulator.Also, if you don't know what port to set, check firebase.json & look for Firestore port there.
tjad commentedon Jul 19, 2022
Yeah, I'm also looking for how to connect the auth utility of firebase-admin?
I tried the docs way of doing it with
FIREBASE_AUTH_EMULATOR_HOST. I get no errors, however when trying to utilize the sdk, I get "Error while making request: getaddrinfo EAI_AGAIN http. Error code: EAI_AGAIN"floer32 commentedon Jul 26, 2022
ℹ️ If you're using Firebase Cloud Storage rather than Firestore, you may need to set
FIREBASE_STORAGE_EMULATOR_HOSTrather thanFIRESTORE_EMULATOR_HOST.tjad commentedon Jul 29, 2022
I managed to get this working after a bit of debugging. As per docs FIREBASE_AUTH_EMULATOR_HOST does work. You should not include the protocol part of the url (http/https)
e.g: localhost:9099
not http://localhost:9099
export FIREBASE_AUTH_EMULATOR_HOST=localhost:9099johnnyoshika commentedon Aug 17, 2022
Can we connect to the emulator without relying on an environment variable? The reason why I ask is that I'd like to create 2 instances of the Firesbase Admin app: 1 for Cloud Firestore and 1 for Emulator Firestore so that I can copy data between the 2. For example:
johnnyoshika commentedon Aug 18, 2022
The only way I can connect to both the emulator and cloud Firestore in the same script is to use
@firebase/testingfor the emulator connection andfirebase-adminfor the cloud connection.johnnyoshika commentedon Nov 21, 2022
For anyone else who wants to connect to both the emulator and cloud Firestore at the same time, you can do this. It turns out that
process.env.FIREBASE_AUTH_EMULATOR_HOSTandprocess.env.FIRESTORE_EMULATOR_HOSTare only referenced when initializing the app.cedvdb commentedon Jun 11, 2023
Where are all those variables documented ?
FIREBASE_AUTH_EMULATOR_HOST,FIRESTORE_EMULATOR_HOSTfouchekeagan commentedon Jun 12, 2023
@cedvdb they are documented in both GCP and Firestore docs. I've personally found GCP docs to be more technical, while the Firestore docs are more often what you are looking for/will use. Below I linked to the Firestore docs.
These are both sub-sections of the larger documentation for the entire Emulator Suite, I recommend reading everything in the section to strengthen your understanding of the whole Emulators Suite - even when you are confident with the technology, since the docs are often updated with new features.
dominics commentedon Oct 26, 2023
This is true for Firestore, but not true for Firebase Auth (
process.env.FIREBASE_AUTH_EMULATOR_HOST).Specifically, the
AuthResourceUrlBuilder(among other things) callsuseEmulator(), which checksprocess.env.FIREBASE_AUTH_EMULATOR_HOSTagain (e.g. for deciding the URL format). It does this on many (or all?) of the Firebase Auth methods, not just during app (or auth service) initialization.This makes it really hard to run an emulated and non-emulated instance of Firebase Auth from this admin SDK side-by-side in the same process (because both instances are looking at the same piece of global state regularly)
mtrabelsi commentedon Dec 30, 2023
this worked for me for BOTH auth and firestore!:
Amirjab21 commentedon Apr 3, 2024
This emulator should be its own npm package - having to add a global package just to test database is not ideal.
maxiedaniels commentedon May 31, 2024
Ah very nice I was trying to figure out auth emulator for my backend, this answers it. But one question - do I still have to provide a valid config? I was under the impression the emulator of this was so I didn't have to create a second Firebase app for development..
falk-stefan commentedon Oct 16, 2024
How can I verify that the emulator is actually being used?
I am sending a JWT token from my web client (it's using the Firebase Auth Emulator) to my backend.
However, whily calling
verifyIdToken(), I am getting the following error:Does this imply, that
firebase-adminis not using the emulator?I am running the server with
bespy commentedon Jan 17, 2025
Just supplying a stub for the Credential interface works, as the token is never verified.
credit: #2034