When working with Node.js, I occasionally stumble upon on "Port is already in use" or "Port is already allocated" when a previous process failed to clean up successfully when closed. (Nuxt, looking at you! ๐)
Finding the Culprit
You can use the "list open files" command, as everything is regarded as a file in UNIX-based operating systems. lsof
and use it to list any processes listening on the corresponding port. (port 3000 in the below example).
The -i
will show all processes that are associated with the network, such as ports.
$ sudo lsof -i:3000
Running the command will give you a list of processes running. Take note of the PID as this is what you will need to kill the process in a moment.

Killing the Process
Once you have identified the process that you wish to kill, refer to the PID and replace the below "PID" together with the kill
command (-9 sends a SIGKILL signal).
$ sudo kill -9 PID

And that's it. You should be able to rerun your process and listen on your intended port again.
Open Ports for MacOS
An easier way to go about viewing all open ports for macOS is by using Open Ports for macOS by Sumbit Labs. It's also super easy to kill a process on a port.

Open Ports lives in your menu bar giving you quick and instant access to all open ports. The cool thing about this is not only do you get the ability to kill any dangling processes, but additional support for opening the the URL within your browser.
For $2.99, it's not a bad little deal!
If you'd like to pick up a copy, you can get yours here.