This is my first time working with Node.js and I ran into this problem:
I have started a Node server through the plugin of an IDE. Unfortunately, I cannot use the IDE's terminal. So I tried to run the script from the command line.
This is the problem – I am using the Express module and my app is listening some port (8080). When I start the app from the command line, it throws this error:
events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:770:11)
at HTTPServer.Server._listen2 (net.js:910:14)
at listen (net.js:937:10)
at HTTPServer.Server.listen (net.js:986:5)
at Object.<anonymous> (C:\xampp\htdocs\node\chat\app.js:5:5)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)
Even though I am not very sure what this error could be I assumed that it's because the app is listening on a port which is already in use. So I did:
netstat -an
I can see
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING
It's because the Node server is already started when I tried to start it from the IDE.
So I want to know, how can I stop all server instances? Also if you can tell me how to detect what's running on a port and kill it.
Best Answer
Windows Machine:
Need to kill a Node.js server, and you don't have any other Node processes running, you can tell your machine to kill all processes named
node.exe
. That would look like this:And if the processes still persist, you can force the processes to terminate by adding the
/f
flag:If you need more fine-grained control and need to only kill a server that is running on a specific port, you can use
netstat
to find the process ID, then send a kill signal to it. So in your case, where the port is8080
, you could run the following:The fifth column of the output is the process ID:
You could then kill the process with
taskkill /pid 14828
. If the process refuses to exit, then just add the/f
(force) parameter to the command.MacOS machine:
The process is almost identical. You could either kill all Node processes running on the machine:
Or also as alluded to in @jacob-groundwater's answer below using
lsof
, you can find the PID of a process listening on a port (pass the-i
flag and the port to significantly speed this up):The process ID in this case is the number underneath the PID column, which you could then pass to the
kill
command:If the process refuses to exit, then just use the
-9
flag, which is aSIGTERM
and cannot be ignored:Linux machine:
Again, the process is almost identical. You could either kill all Node processes running on the machine (use
-$SIGNAL
ifSIGKILL
is insufficient):Or also using
netstat
, you can find the PID of a process listening on a port:The process ID in this case is the number before the process name in the sixth column, which you could then pass to the
kill
command:If the process refuses to exit, then just use the
-9
flag, which is aSIGTERM
and cannot be ignored: