Port is Already in Use (MacOS + Linux)

Harry 2 min read
Port is already in use (troubleshooting)
Table of Contents

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
Process successfully killed

And that's it. You should be able to rerun your process and listen on your intended port again.

Open Ports for MacOS

The below contains affiliate links where I may get a small commission for anything purchased via my link. Check my affiliate policy for further information.

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 for MacOS by Sumbit Labs

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.

Open Ports for macOS
๐Ÿ’ป Lives in your menu bar. List, browse, copy, terminate or kill open ports. All from your menu bar.๐Ÿณ Using Docker for Mac? Open Ports will let you manageโ€ฆ

Harrk Blog

Programming nonsense, tutorials, and random musings about games and technology.

Great! Youโ€™ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Harrk Blog.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.