My port is taken

Matt
Nov 12, 2020

Running a web app requires a free port. Let’s explore this.

When running a node app the code will try to bind to a port on the operating system for it to receive network data. So, how do we know if a port is in use? Well, on my mac i can type this in the terminal (for your OS you will have to find a similar command)

lsof -nP -iTCP:$PORT | grep LISTEN

where, in this example i can see the port 8084 is listening/in use:

lsof -nP -iTCP:8084COMMAND   PID    USER   FD   TYPE  DEVICE           SIZE/OFF NODE NAME
node 34286 coderguy 23u IPv6 0x7a4e2bdc43577e11 0t0 TCP *:8084 (LISTEN)

A common problem is you want to run this app again but the port is in use somewhere (maybe you have multiple terminals and can’t find it).

In this case you need to terminal the process listening to the port:

kill -9 <PID>

The pid is the second field of the lsof command above, so 2321 in our example.

If you are using docker, killing the pid will kill docker which might not be what you want.

--

--

Matt
0 Followers

A curious mind in the tech field. #Question-everything