This article provides instructions on how to manually kill a process when the "EADDRINUSE: address already in use" error occurs in a Node application on Mac/Linux and Windows.
Abstract
The "EADDRINUSE: address already in use" error in a Node application occurs when the previous application did not shut down properly, and the new one is trying to use the same port. The article explains that the proper fix for the application would be to handle crashes and kills using process.on('uncaughtException') and process.on('SIGTERM') respectively. However, when this issue has already happened, the process needs to be killed manually. The article provides step-by-step instructions on how to find the process id (PID) associated with the port and kill the process on Mac/Linux and Windows.
Opinions
The article assumes that the reader has a basic understanding of Node applications and command-line interfaces.
The article provides clear and concise instructions on how to resolve the "EADDRINUSE" error.
The article emphasizes the importance of properly handling crashes and kills in Node applications to prevent this error from occurring.
The article provides solutions for both Mac/Linux and Windows users, making it accessible to a wider audience.
The article suggests using the sudo keyword if the user encounters permissions errors while trying to kill the process.
The article recommends using the -9 option to make sure the process dies immediately.
The article provides an alternative solution for Windows users who prefer using the Task Manager application instead of the command prompt.
How to kill server when seeing “EADDRINUSE: address already in use”
A tutorial on how to kill the process manually when “EADDRINUSE” happens on Mac/Linux and Windows.
The Problem
When trying to restart a Node application, the previous one did not shut down properly, and you may see a “listen EADDRINUSE: address already in use” error such as:
The cause behind this issue
The reason behind this is that
process.on('exit', ...) isn't called when the process crashes or is killed. It is only called when the event loop ends, and since server.close()sort of ends the event loop (it still has to wait for currently running stacks here and there) it makes no sense to put that inside the exit event.
Solution
The proper fix for the application would be
On crash, do process.on('uncaughtException', ..)
And on kill do process.on('SIGTERM', ..)
When this EADDRINUSE issue has already happened, in order to resolve it, you need to kill the process manually. In order to do that, you need to find the process id (PID) of the process. You know the process is occupying a particular port on your machine or server.
Kill the process manually
For Mac/Linux
To find the process id (PID) associated with the port
Use -9 option to make sure the process dies immediately
If you get permissions errors, you may need to use the sudo keyword, for example:
sudo kill -944475
For Windows
Solution 1: Task Manager
Open the Task Manager application (taskman.exe), from either the Processes or Services tab sort by the PID column. To display the PID column, right-click the header row and select PID from the list. Right-click the process you want to stop and select End task.
Solution 2: Use Command prompt
Open a CMD window in Administrator mode by navigating to Start > Run > type cmd> right-click Command Prompt, then select Run as administrator.
Use the netstat command lists all the active ports. The -a switch displays all ports in use, not just the ports associated with the current user. The -n option stops a hostname lookup (which takes a long time). The -o option lists the process ID that is responsible for the port activity. The findstr command matches the header row that contains the PID string, and the port you are looking for, in a port format with the preceding colon, is :3000.
C:\Users\admin>netstat -ano|findstr "PID :3000"Proto Local Address Foreign Address State PID
TCP0.0.0.0:30000.0.0.0:0 LISTENING 18264
To kill this process (the /f is force):
taskkill /pid 18264 /f
Enjoy!
And that’s about it. Thanks for reading
Level Up Coding
Thanks for being a part of our community! Level Up is transforming tech recruiting. Find your perfect job at the best companies.