Terminate a running process with NodeJS

Feburary 11, 2021

I was recently trying to kill a running process with the terminal on my local machine.
I’d been running a nodejs service in the background that was hanging and wouldn’t stop with the normal kill command, it was going to take a bit more effort to free up the port it held.

Like with most terminal commands, I couldn’t exactly remember the options that it took to force kill the process. I had to do a quick Google search to find the exact command I was looking for.

There are a few ways of doing it but here’s the usual way of terminating a running process.

Using Bash/Terminal

Get the process attached to the port you need to free up (e.g. port 3000)

lsof -i :3000

Kill the process using the process ID retrieved from the above command

kill -9 <process ID>

The above will kill the process immediately (SIGKILL) and should make the desired port available.

Using NodeJS

After doing some research, I came across a new way of killing processes that I’ve since started using.

There is one requirement, it needs NodeJS to be present, If you have it installed, you can run the following command and it will kill any running process on the port you specify.

npx kill-port 3000