Generally when I try to run a web server on an existing port, it will tell me that the port is already in use and ask if I want to use another one:
Something is already running at port 8080 ? Would you like to run the app at another port instead? › (Y/n)
But sometimes, usually for older servers, when the port is in use I’ll get an error like:
Fatal error: Port 8080 is already in use by another process.
Typically this error simply means that I have to stop a duplicate server. But recently I was having problems with Gatsby not completely shutting down when I stopped the server. So even though the web server technically wasn’t running, the port was still in use. And I had no idea how to kill the process running on the port.
kill $(lsof -t -i:8080)
So I did that the first time. And honestly I wasn’t sure if I was opening a backdoor for someone to use my machine to mine Bitcoin. 😂
But the next time I needed to kill a process running on a port, I had to google again to find that same Stack Overflow question. My command line skills are improving day-by-day, but there’s no way I’m going to be able to remember that command.
npx kill-port 8080
That’s it! It’s slower than the
npx has to temporarily install the
kill-port package. But it’s 1000x times easier for me to remember. I took a peak at the source code for
kill-port and under the hood the package is using
lsof of course! The
kill-port package is just providing a more approachable abstraction in my opinion.
Keep learning my friends. 🤓