Fix Node.js Port In Use Error
Adam C. |

Sometimes the Node.js application does not close itself when you close the IDE, then when you try to start the application later, it will not start because the port is in use. It's easy to fix, but you need to remember the command to find the process and then kill it, which I cannot remember, and have to Google it every time. So annoying until I figure out the better way.

Photo by Srini Somanchi on Unsplash

By the way, in my case, the Node application is Feathers.js, which I use in most of my projects. For some reason, it does not shut down after I close the IDE. 

Here is what I am doing now.  I installed the “kill-port”, and in my “dev” scripts, I use:

   "dev": "npx kill-port 3333 & nodemon src/",

Note that my FeathersJS running on port 3333, so I try to kill it every time I run npm run dev.

It should be fine if the FeathersJS is not running. I can just ignore the message:

Could not kill process on port 3333. No process running on port.

No more annoying “Port In Use” error.

UPDATE (07/06/2022)

As I tested, “npx kill-port 333” and “nodemon src/” are running in parallel, which is an issue causing crashes. So I revised my scripts to be:

"dev":  "nodemon src/",
"shundown": "npx kill-port 3333"

Which means I only run “npm run shutdown” when there is a Port in Use issue.