Chris Mendez in NodeJS, DevOps

NodeJS: How to fix localhost Error: listen EADDRINUSE

If you're using NodeJS, sometimes you'll get an error like this:

Error: listen EADDRINUSE :::5000
    at Object.exports._errnoException 
    at exports._exceptionWithHostPort 
    at Server.setupListenHandle [as _listen2]
    at listenInCluster 
    at Server.listen 

What this error means is that you're trying to bind the app but the server is already in use. The only way to to first kill the server. Here's how:

Process Scanning

You can always use good ol' grep to find which process is already running. First run process then use grep to filter out any process that has the name "node". This command contains an extra grep -v grep so that it ignores the grep process.

Method 1

ps | grep node | grep -v grep

Method 2

ps -ax | grep node

Port Scanning

Method 1

You can search using the tcp port.

lsof -i tcp:5000

Method 2

You can also use netstat with grep to find the process with the port.

netstat -punta | grep 5000

Stop Process

Once you find the culprit, look for the PID. This is a number you will use to stop the process.

kill -9 <PID number>