Unable to connect to firebase emulator suite with exec
Asked Answered
H

6

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.

Harrie answered 2/4, 2020 at 16:14 Comment(4)
It sounds like you have another emulator process running.Roi
I tried restarting VS code. But still the same case. Let me kill the ports and see.Harrie
@DougStevenson Still the same case :|Harrie
npx kill-port 8080 solved this error for me: Port 8080 is not open on localhost, could not start firestore emulatorMonnet
B
30

lsof command is not availalble on windows powershell.

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

Biocatalyst answered 20/5, 2021 at 14:28 Comment(0)
K
14

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

lsof -ti tcp:8080 | xargs kill

Reason: Failed quitting the previous firebase emulator.

Kalie answered 2/11, 2020 at 16:32 Comment(1)
'lsof' is not recognized as an internal or external command, operable program or batch file. Any tips on that?Hagerman
W
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.

Wager answered 2/4, 2020 at 17:30 Comment(9)
✔ firestore: firestore emulator started at localhost:8080 -> This means its accessible right?Harrie
I tried changing to 9081 and still is throwing same error.Harrie
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.Wager
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:startHarrie
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.Wager
I restarted the system as well. It seems like it should work, but not sure why its failing.Harrie
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).Wager
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.Harrie
Have updated my answer; this issue seems to be specific to firepit & jest.Wager
A
0

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

Arrears answered 18/6, 2021 at 3:34 Comment(0)
C
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
Cab answered 30/5, 2023 at 22:1 Comment(0)
R
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

Ranch answered 22/8, 2023 at 23:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.