How to end a process by port number
Have you ever tried to start a service and then discover that something is already running on that port, but you aren't sure what?
You might have saw an error like
Error: listen EADDRINUSE: address already in use :::5000 and wondered what to do next.
Well then I have a solution for you! There is an incredibly useful package called kill-port that allows you to kill a process on a given port.
It's super easy to use. All you have to do is run the following command:
npx kill-port 5000
That's it! Now you can start your service on port 5000. I should mention this isn't "THE" way to end a process by port
number, but the
npx command is pretty convenient as it doesn't require you to install anything. I have seen it fail before, and I had to terminate the process another way - but 60% of the time it works everytime! 🤣
If you run into that situation, you can look up how to terminate a process by port number on your operating system. I won't go into that here, but the important takeaway here is: if kill-port doesn't work, you can always terminate the process another way!
Wait... What if I want to see which process is running on that port before terminating it? 🤔
Naturally, you might want to see which process is running on a given port before you terminate it. If so, you can use the following commands to see which process is running on a given port.
lsof -i :5000
netstat -ano | findstr :5000
5000 with the port number you want to check, of course! 🤠