23

Started firebase project emulators (which uses cloud functions and firestore) with below command

firebase emulators:start

It runs successfully and gives me a path to connect to the functions and shows a local host url for firestore too.

Then, to execute my jest tests, ran the below command

firebase emulators:exec --only firestore jest

As per the documentation, to connect to local firstore, we need to use exec. But its throwing below error.

i  emulators: Starting emulators: firestore
⚠  emulators: emulator hub unable to start on port 4400, starting on 4401
✔  hub: emulator hub started at http://localhost:4401
i  Shutting down emulators.
i  Stoppping emulator hub
⚠  Port 8080 is not open on localhost, could not start firestore emulator.
i  To select a different host/port for the emulator, update your "firebase.json":
    {
      // ...
      "emulators": {
        "firestore": {
          "host": "HOST",
          "port": "PORT"
        }
      }
    }
i  Shutting down emulators.
Error: Could not start firestore emulator, port taken.

This error is thrown everytime when I run exec command. Can someone point out what could be wrong?

Edit: Logs from firebase emulators:start

firebase emulators:start
i  emulators: Starting emulators: functions, firestore, hosting, pubsub
✔  hub: emulator hub started at http://localhost:4400
⚠  Your requested "node" version "8" doesn't match your global version "10"
✔  functions: functions emulator started at http://localhost:5001
i  firestore: Serving ALL traffic (including WebChannel) on http://localhost:8080
⚠  firestore: Support for WebChannel on a separate port (8081) is DEPRECATED and will go away soon. Please use port above instead.
i  firestore: firestore emulator logging to firestore-debug.log
✔  firestore: firestore emulator started at http://localhost:8080
i  firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
i  hosting[website]: Serving hosting files from: public
✔  hosting[website]: Local server: http://localhost:5000
i  hosting[admin]: Serving hosting files from: public
✔  hosting[admin]: Local server: http://localhost:5005
i  hosting[b2b]: Serving hosting files from: public
✔  hosting[b2b]: Local server: http://localhost:5006
i  hosting[b2c]: Serving hosting files from: public
✔  hosting[b2c]: Local server: http://localhost:5007
i  hosting[sdk]: Serving hosting files from: public
✔  hosting[sdk]: Local server: http://localhost:5008
✔  hosting: hosting emulator started at http://localhost:5000
i  pubsub: pubsub emulator logging to pubsub-debug.log
✔  pubsub: pubsub emulator started at http://localhost:8085

Update

With a fresh start also the mentioned error is shown. But killing the port made it work.

Added below in scipts part of package.json

"kill": "npx kill-port 5000 5001 8080 8085 4000 9229"

and run

npm run kill

Here all above listed ports aren't reqired. Just 8080 might work in your case but I have other ports too being used by the emulator.

Ayyappa
  • 1,876
  • 1
  • 21
  • 41

6 Answers6

29

lsof command is not availalble on windows powershell.

A better cross-platform solution is: npx kill-port 8080

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
13

Type this in your terminal, where 8080 is your port number:

lsof -ti tcp:8080 | xargs kill

Reason: Failed quitting the previous firebase emulator.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
2

There is an issue filed for firepit (the standalone CLI installable with curl): #1868

Installing it as Node package may help: npm install -g firebase-tools

If this should already be the case, you probably should file an issue there.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • ✔ firestore: firestore emulator started at http://localhost:8080 -> This means its accessible right? – Ayyappa Apr 02 '20 at 17:36
  • I tried changing to 9081 and still is throwing same error. – Ayyappa Apr 02 '20 at 17:39
  • It seems that the previous instances were not shutdown properly; because the log also complains about other ports; eg. `⚠ emulators: emulator hub unable to start on port 4400, starting on 4401`. `netstat -lntu` would show what is going on (at least on Linux); Windows should have a similar command. – Martin Zeitler Apr 02 '20 at 17:40
  • 1
    lsof -t -i tcp:4400 | xargs kill -9 | firebase emulators:exec --only firestore jest -> I tried killing the open port and ran exec, what it did was, it killed the process started with firebase emulators:start – Ayyappa Apr 02 '20 at 17:43
  • 1
    As a workaround this may suffice; still the question why it doesn't terminate the instances properly, so that it would work as expected. Restarting VS code is rather pointless, because this will not affect the servers which occupy the ports. – Martin Zeitler Apr 02 '20 at 17:48
  • I restarted the system as well. It seems like it should work, but not sure why its failing. – Ayyappa Apr 02 '20 at 18:09
  • If it doesn't work instantly after a restart, there likely is another service which already uses `:8080` (because there should not be any other emulator instance listening on that port). Try running `netstat -lntu` after the restart, to see what this may be. There's certainly two services trying to exclusively bind to the same port (these must not be shared; one port = one service). – Martin Zeitler Apr 02 '20 at 18:15
  • I tried with a random port in firebase.json and called emualtors:start. And while its running i ran emulators:exec. It throwed same error that the random port is not open. – Ayyappa Apr 02 '20 at 18:42
  • Have updated my answer; this issue seems to be specific to firepit & jest. – Martin Zeitler Apr 03 '20 at 02:49
0

I closed the terminal, then opened it again, launched command again and it works

Liker777
  • 2,156
  • 4
  • 18
  • 25
0

You can find PID and after kill process:

> netstat -lnp | grep 8080
Proto Recv-Q Send-Q Local Address  Remote address     State       PID/Program name
tcp6       0      0 127.0.0.1:8080  :::*              LISTENING   52196/java

> kill 52196
vctlzac
  • 837
  • 2
  • 16
  • 38
0

Many answers here are describing a useful way to recover from this error, by terminating things on already open ports.

To solve this problem, it's necessary to tell jest to kill any processes that were opened, by supplying the --forceExit option.

This is demonstrated in the official Firebase quickstart-testing repo