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.

Firestore emulator: Error: 14 UNAVAILABLE

See original GitHub issue

Firebase CLI 7.10.0 Firestore emulator 1.10.2

index.js:

const functions = require ('firebase-functions');
const admin = require ('firebase-admin');
admin.initializeApp();
const firestore = admin.firestore();

exports.helloWorld = functions.https.onRequest (async (req, res) => {
  const docRef = firestore.collection ('col').doc ('doc');
  await docRef.set ({foo: 'bar'});
  const docSnap = await docRef.get();
  const wroteCorrectly = docSnap.get ('foo') == 'bar';
  res.send (wroteCorrectly ? 'Worked' : 'Did not work');
});

Expected: ‘Worked’. Instead: Error: 14 UNAVAILABLE: No connection established (etc.)

  • FIRESTORE_EMULATOR_PORT is set (8080).
  • GOOGLE_APPLICATION_CREDENTIALS are set.
  • This runs fine on the remote Firestore. Only a problem when using local emulator.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
samtsterncommented, Jan 3, 2020

@bwhrsc thanks for your feedback, it shows that some things are not documented as clearly as they could be.

FIRESTORE_EMULATOR_HOST is an environment variable you can set to tell Firebase SDKs to talk to the emulator rather than to production. It does not control where the emulator runs. To set that, you change a value in firebase.json:

{
  "emulators": {
     "firestore": { "port": 8080 }
  }
}

So when you want to run tests against the emulator you can use one of two methods.

Method 1: Manual In one terminal, run the emulators:

$ firebase emulators:start

In another terminal, run your tests and set the env var:

$ export FIRESTORE_EMULATOR_HOST="localhost:8080"
$ npm run test

Method 2: Automatic In your terminal, run:

$ firebase emulators:exec "npm run test"

The above command will do the following things, in order:

  1. Start up all the emulators (and wait for them to confirm they are running)
  2. Launch a new subprocess with the proper environment variables (FIRESTORE_EMULATOR_HOST, etc) setup
  3. Run your test command (npm run test or anything else) in that subprocess
  4. Wait for your test command to finish
  5. Cleanly shut down all the emulators and exit

So I believe that should fix your issue. Let me know if something is still not working!

0reactions
mrspencecommented, May 12, 2022

Tip: Try removing the protocol from your hostname

i.e.:

functionsInstance.useEmulator(`http://${emulatorHostname}`, 5001)

becomes

functionsInstance.useEmulator(emulatorHostname, 5001)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: 14 UNAVAILABLE: No connection established - Cloud ...
I'm trying to use the admin SDK for firebase on my machine. When I try to get the users collection I get Error:...
Read more >
Install, configure and integrate Local Emulator Suite - Firebase
The Firebase Local Emulator Suite can be installed and configured for different prototype and test environments, anything from one-off prototyping sessions ...
Read more >
Port 8080 is not open on localhost, could not start Firestore ...
This error is because of the failed quitting from firebase emulator. You already have the process of previous firebase emulator.
Read more >
Incidents - Firebase Status Dashboard
For incidents related to Cloud Functions, Cloud Firestore and Cloud ... Firebase Messaging is experiencing periods of errors on app registrations and ...
Read more >
Errors and Error Handling | Cloud Datastore Documentation
Error Codes. When a Firestore in Datastore mode request is successful, the API will return an HTTP 200 OK status code along with...
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