Mohit Gupta Mohit Gupta - 4 months ago 38
Node.js Question

started learning node cluster getting error

I tried this as my first code and i am getting error

const cluster = require('cluster');
const http = require('http');
const numCPUs = 4;

if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('online', function(worker) {
console.log('Worker ' + worker.process.pid + ' is online');
});

cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});

} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
}


This is the Error i am getting :-

Worker 11056 is online
Worker 11057 is online
Worker 11058 is online
Worker 11059 is online
events.js:141
throw er; // Unhandled 'error' event
^

Error: bind EADDRINUSE null:8000
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at cb (net.js:1302:16)
at rr (cluster.js:594:14)
at Worker. (cluster.js:564:9)
at process. (cluster.js:714:8)
at emitTwo (events.js:92:20)
at process.emit (events.js:172:7)
at handleMessage (internal/child_process.js:689:10)
at Pipe.channel.onread (internal/child_process.js:440:11)
events.js:141
throw er; // Unhandled 'error' event
^

Error: bind EADDRINUSE null:8000
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at cb (net.js:1302:16)
at rr (cluster.js:594:14)
at Worker. (cluster.js:564:9)
at process. (cluster.js:714:8)
at emitTwo (events.js:92:20)
at process.emit (events.js:172:7)
at handleMessage (internal/child_process.js:689:10)
at Pipe.channel.onread (internal/child_process.js:440:11)
events.js:141
throw er; // Unhandled 'error' event
^

Error: bind EADDRINUSE null:8000
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at cb (net.js:1302:16)
at rr (cluster.js:594:14)
at Worker. (cluster.js:564:9)
at process. (cluster.js:714:8)
at emitTwo (events.js:92:20)
at process.emit (events.js:172:7)
at handleMessage (internal/child_process.js:689:10)
at Pipe.channel.onread (internal/child_process.js:440:11)
worker 11056 died
worker 11057 died
worker 11058 died
events.js:141
throw er; // Unhandled 'error' event
^

Error: bind EADDRINUSE null:8000
at Object.exports._errnoException (util.js:870:11)
at exports._exceptionWithHostPort (util.js:893:20)
at cb (net.js:1302:16)
at rr (cluster.js:594:14)
at Worker. (cluster.js:564:9)
at process. (cluster.js:714:8)
at emitTwo (events.js:92:20)
at process.emit (events.js:172:7)
at handleMessage (internal/child_process.js:689:10)
at Pipe.channel.onread (internal/child_process.js:440:11)
worker 11059 died

Answer

EADDRINUSE means that something else is already bound to the port/IP address combination that you are trying to use.

Things to check:

  • Do you already have some other process binding to port 8000? If so, perhaps change the port number in your code here.

  • Do you have another instance of this code running? (On UNIX-like operating systems, perhaps use ps with appropriate options to find it.) If so, terminate the other instance. (On UNIX-like operating systems, you might use kill or pkill with appropriate options.)

Comments