[Solved-5 Solutions] How to fix Error listen Eaddrinuse while using nodejs
Error Description:
If we run a server with the port 80, and we try to use xmlHTTPrequest we get this error: Error: listen EADDRINUSE
- The server is:
net.createServer(function (socket) {
socket.name = socket.remoteAddress + ":" + socket.remotePort;
console.log('connection request from: ' + socket.remoteAddress);
socket.destroy();
}).listen(options.port);
click below button to copy the code. By - JavaScript tutorial - team
And the request:
varxhr = newXMLHttpRequest();
xhr.onreadystatechange = function() {
sys.puts("State: " + this.readyState);
if (this.readyState == 4) {
sys.puts("Complete.\nBody length: " + this.responseText.length);
sys.puts("Body:\n" + this.responseText);
}
};
xhr.open("GET", "http://wikitechy.com");
xhr.send();
click below button to copy the code. By - JavaScript tutorial - team
Solution 1:
EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.
- In your case, there must be running a server on port 80 already.
- If you have another webserver running on this port you have to put node.js behind that server and proxy it through it.
- You should check for the listening event like this, to see if the server is really listening:
var http=require('http');
var server=http.createServer(function(req,res){
res.end('test');
});
server.on('listening',function(){
console.log('ok, server is running');
});
server.listen(80);
click below button to copy the code. By - JavaScript tutorial - team
Solution 2:
- The killall -9 node, works as expected and solves the problem but you may want to read the answer about why kill -9 may not be the best way to do it.
- On top of that you might want to target a single process rather than blindly killing all active processes.
- In that case, first get the process ID (PID) of the process running on that port (say 8888):
lsof -i tcp:8888
click below button to copy the code. By - JavaScript tutorial - team
This will return something like:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 57385You11uIPv60xac745b2749fd2be30t0 TCP *:ddi-tcp-1(LISTEN)
click below button to copy the code. By - JavaScript tutorial - team
kill -9 57385
click below button to copy the code. By - JavaScript tutorial - team
Solution 3:
- Skype will sometimes listen on port 80 and therefore cause this error if you try to listen on port 80 from Node.js or any other app.
- You can turn off that behaviour in Skype by accessing the options and clicking Advanced -> Connection -> Use port 80 (Untick this)
Solution 4:
- You should try killing the process that is listening on port 80.
- kill all the node apps running. You might not want to do that. With this command you can kill only the one app that is listening on a known port.
If using unix try this command:
sudo fuser -k 80/tcp
click below button to copy the code. By - JavaScript tutorial - team
Solution 5:
Simply close the terminal and open a new terminal and run
node server.js