Mostly in development machines, we tend to leave out the node server running, and close the terminal. This keeps the node server running, and when we try to restart the application, it throws the “EADDRINUSE: address already in use” error.
Error: listen EADDRINUSE: address already in use :::8080
at Server.setupListenHandle [as _listen2] (net.js:1258:14)
at listenInCluster (net.js:1306:12)
at Server.listen (net.js:1394:7)
This can be fixed by first finding out which PID the process is using and then kill it.
Please note that the following commands were run on Ubuntu 18.04 LTS
For Windows or Mac, the process remains the same, while the commands might be a little different.
Will add them up when time permits.
Ubuntu
To find the PID,
lsof -i TCP:8080 | grep LISTEN
For me, the port that was in use was 8080. You can replace that with any port you want.
The result will show something like:
node 2464 murari 21u IPv6 4392639 0t0 TCP *:http-alt
Note the second column of the result, which is the PID, and required to stop the running process.
kill -9 2464
2464 is the PID from the previous result, and running the above line will kill the process running on the given port.
Windows
You might need to open the CMD in Administrator mode
To find the PID
netstat -ano | findstr :8080
Result would be something like:
TCP 0.0.0.0:8080 0.0.0.0:0 LISTENING 4744
In windows, the last column referes to the PID, and can be used to forcefully terminate the process. To terminate, run
taskkill /PID 4744 /F
Confirm
You can check if the port has got free by running the first command again, and you should get no results, making sure that the EADDRINUSE EADDRINUSE error is resolved.
To be able to use multiple node versions on the system, we can use NVM (Node Version Manager). You can find more information on how to install NVM here.
Some additional commands
Check the history with the given command. Can be used without grep
history | grep some_command
Kill all the process with name 'conky'
kill -9 $(pidof conky)