How to kill server when seeing “EADDRINUSE: address already in use”
https://stackoverflow.com/questions/4075287/node-express-eaddrinuse-address-already-in-use-kill-server
https://levelup.gitconnected.com/how-to-kill-server-when-seeing-eaddrinuse-address-already-in-use-16c4c4d7fe5d
================================
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:
listen EADDRINUSE: address already in useThe 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 sinceserver.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
⇒ lsof -i tcp:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 44475 chen5 31u IPv4 0x8b1721168764e4bf 0t0 TCP *:strexec-s (LISTEN)
Then to kill the process
kill -9 44475
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 -9 44475
=============================
First, you would want to know which process is using port 3000
sudo lsof -i :3000
this will list all PID listening on this port, once you have the PID you can terminate it with the following:
kill -9 {PID}
=============================
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.
source from Google SearchUse 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
TCP 0.0.0.0:3000 0.0.0.0:0 LISTENING 18264
To kill this process (the /f is force):
taskkill /pid 18264 /f