When a node application crashes unexpectedly, you might get a EADDRINUSE, Address already in use
error when you try to restart it. This means that the port you’re using is already being used by the crashed application. What you need to do is fun the PID of that process, and then kill it. There’s more than one way to skin that cat, but I like to use netstat
. N.B. You’ll need to run this as sudo
for it to work.
sudo netstat -tulpn
This will produce something like the following. You want to look in the Local Address
column for the port your app was using. Suppose you’re using port 3000, which appears on the next to last line. The PID for that process (and the name of the program using that process as a sanity check) can be found on the last column.
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 3374/snmpd tcp 0 0 0.0.0.0:775 0.0.0.0:* LISTEN 3137/rpc.statd tcp 0 0 0.0.0.0:53644 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 3094/portmap tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3679/sshd tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 25011/node tcp 0 0 :::22 :::* LISTEN 3679/sshd
Then, to kill that process you would say,
kill -9 25011
I’ve seen other people use these solutions on Stack Overflow. Notably, running
ps aux | grep "node"
Which returns,
root 25011 0.2 0.0 1966296 8 pts/16 Sl+ Jul20 8:21 node app.js
In this case, the PID is found in the second column.