How to Fix Error: listen EADDRINUSE: address already in use in Node.js

Error: listen EADDRINUSE: address already in use occurs in Node.js when you “try to restart a Node application, and the previous one did not shut down properly.”

listen EADDRINUSE - address already in use in Node

How to fix the error?

Here are some steps to diagnose and fix the issue.

Check Running Processes

You can use the below command to see if any process is running on the port you’re interested in.

Replace PORT_NUMBER with the port you are trying to use.

lsof -i :PORT_NUMBER

The above command will show you which process is using that port.

If you find a process that shouldn’t be running, you can kill it with:

kill -9 PROCESS_ID

Where PROCESS_ID is the ID of the process you want to terminate.

Ensure Proper Shutdown

If you frequently encounter this error, it might be because your Node.js application isn’t shutting down properly. Ensure you handle exit signals (SIGINT, SIGTERM) to shut down your server gracefully.

Use a Different Port

You can modify your Node.js application to use a different port. This involves updating a configuration file or changing the value where you call listen() in your code.

That’s it!