[Node.js] Error: bind EADDRINUSE

Tadashi Shigeoka ·  Sun, January 18, 2015

A failure occurred in Node.js (Express) where bind EADDRINUSE errors frequently appeared after forever restart.

Error: bind EADDRINUSE is an error that occurs when that port is already being used. I’ll make a note of the solution for people who have suffered from the same failure.

Error Message

Error: bind EADDRINUSE
    at errnoException (net.js:904:11)
    at net.js:1084:30
    at Object.12:1 (cluster.js:592:5)
    at handleResponse (cluster.js:171:41)
    at respond (cluster.js:192:5)
    at handleMessage (cluster.js:202:5)
    at process.EventEmitter.emit (events.js:117:20)
    at handleMessage (child_process.js:318:10)
    at child_process.js:392:7
    at process.handleConversion.net.Native.got (child_process.js:91:7)
    at process. (child_process.js:391:13)
    at process.EventEmitter.emit (events.js:117:20)
    at handleMessage (child_process.js:318:10)
    at Pipe.channel.onread (child_process.js:343:11)

First, check with the netstat command whether the port used by the node.js server process is being used by other processes.

$ netstat -nltp | grep 3000
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp        0      0 0.0.0.0:3000                0.0.0.0:*                   LISTEN      6653/phantomjs      

This time, phantom.js seemed to be running on port 3000. Although we were using phantom.js, we had configured it to start on other port numbers, so the reason it was using port 3000 is unknown. If it’s safe to stop the process, identify the PID (process number) of phantom.js with ps aux | grep phantomjs and terminate the process with the kill command.

Finally, start the node server with forever, pm2, or the node command, and confirm that Error: bind EADDRINUSE is not output in the error log to complete the process.


Reference Information

How to handle warn - error raised: Error: listen EADDRINUSE when developing Node.js - Qiita

node.js - Nodejs application Error: bind EADDRINUSE when use pm2 deploy - Stack Overflow

5 Pitfalls of Linux Socket Programming

That’s all from the Gemba.