Finding and killing Node.js processes on Mac

Port 3000 is already in use. 💣

A quick note to self because I'm tired of re-looking this up every once in a blue moon. Hopefully this helps others who run into this situation.

Sometimes when I go to start a Node.js server on my Mac, Node crashes and tells me:

Port 3000 is already in use

Great! Except I don't have any terminals open with a server running on that port.

In this situation, I don't care what's running on that port. I probably exited from a development environment in a way that didn't let Node.js know I was done. So I want the process currently using port 3000 to just go away.

There seem to be a whoooole bunch of ways to accomplish this task, and the method seems partly dependent on your OS. I'm not going to run through a bunch of explanations or options, I'm just going to note what works for me on Mac.

1. Find the process ID (PID)

% lsof -i :3000
COMMAND   PID    USER    FD  TYPE  #...      
node    36823     ash   29u  IPv6  #... 

2. Kill it!

% kill -9 38951

And that's it.

Now port 3000 is free and I can spin up the development server I want to work on now.

If you want to learn more about what's going on with these commands and what your options are, there are a lot of resources out there of varying utility. I found this article on The Geek Diary to provide just enough detail on options for killing a process.