551

If I run a server with the port 80, and I try to use XMLHttpRequest I am getting this error: Error: listen EADDRINUSE

Why is it problem for NodeJS, if I want to do a request, while I run a server on the port 80? For the webbrowsers it is not a problem: I can surf on the internet, while the server is running.

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); 

And the request:

var xhr = new XMLHttpRequest(); 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://mywebsite.com"); xhr.send(); 
5
  • Are you sure options.port is defined as 80? Is the XHR code running in a browser? Can you run "nc -l 0.0.0.0 80" when this server is not running? Commented Mar 28, 2012 at 0:34
  • See a similar issue at stackoverflow.com/questions/8553957/… Commented Jun 26, 2015 at 12:01
  • Which system are you on? Some systems require sudo if you want to listen to ports below a certain treshold. Commented Jul 30, 2015 at 13:57
  • this problem arises because you either ran your server on that port and you had not closed that port, the error clearly says that port is already in use this happens for me is when I open a new project in vs code without closing other projects(opening by drag and drop) Commented Mar 10, 2019 at 5:47
  • stackoverflow.com/questions/39632667/… Commented Apr 1, 2019 at 9:02

45 Answers 45

1
2
1

While killing the NODE_PORT, it might kill your chrome process or anything that is listening to the same port, and that's annoying.

This shell script may be helpful - in my case the port is 1337 but you can change it anytime

# LOGIC CHROME_PIDS=`pidof chrome` PORT_PIDS=`lsof -t -i tcp:1337` for pid in $PORT_PIDS do if [[ ${CHROME_PIDS} != *$pid* ]];then # NOT FOUND IN CHROME PIDS echo "Killing $pid..." ps -p "$pid" kill -kill "$pid" fi done sails lift # OR 'node app' OR whatever that starts your node exit 
Sign up to request clarification or add additional context in comments.

Comments

1

In my case I use a web hosting but it´s the same in local host, I used:

ps -aef | grep 'node' 

for watch the node process then, the console shows the process with PID. for kill the process you have to use this command:

kill -9 PID 

where PID is the process id from the command above.

Comments

1

The option which is working for me :

Run:

ps -ax | grep node 

You'll get something like:

 8078 pts/7 Tl 0:01 node server.js 8489 pts/10 S+ 0:00 grep --color=auto node kill -9 8078 

Comments

1

I would prefer doing

killall -15 node

because, kill -15 gives process a chance to cleanup itself. Now, you can verify by

ps aux | grep node

Note: If you don't give process a chance to finish what it is currently doing and clean up, it may lead to corrupted files

3 Comments

Glad to see someone recommending SIGTERM over SIGKILL... thanks. This answer could stand to be more comprehensive, but it's still good to see it here.
Ok, If it worked for you. You can upvote this. :) @lindes
I didn't upvote because I don't believe this answer, as currently written, does an adequate job of answering the original question. It seems more of a response to other answers, which would be better done as a comment. Now, if you were to re-write it to more fully address the original question, WHILE using a better choice of signals, I'd gladly give it an upvote. :)
1

This happened to me because I had my server running in another Terminal window. Closing the connection solved the problem.

Comments

1

NOOB ERROR FIX: I'm new to Node.js and setup a webserver listening to port 8080. I ran into the EADDRINUSE error. I tried all the various 'kill -9 node' iterations and kept getting, 'node: no process found'

The problem was, I was calling http.listen(8080); TWICE in the same blob of code. So the first time it was actually working fine, and the second time it threw an error.

If you're getting a 'no process found' response when trying to kill the port, try checking to make sure you're only opening the port once.

Comments

0

if you want to solve this

$ node server events.js:141 throw er; // Unhandled 'error' event ^

Error: listen EADDRINUSE :::3000 at Object.exports._errnoException (util.js:907:11) at exports._exceptionWithHostPort (util.js:930:20) at Server._listen2 (net.js:1250:14) at listen (net.js:1286:10) at Server.listen (net.js:1382:5) at EventEmitter.listen (C:\sendbox\mean\node_modules\express\lib\application .js:617:24) at Object. (C:\sendbox\mean\server.js:28:5) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32)

change your port number to 8000

Comments

0

Windows is always tricky with open source..

change the port simply it works

node-inspector --web-port=8099 

1 Comment

you probably should explain what you are doing.... like they should do an npm install ... then .....
0

EADDRINUSE translates to "The port you are trying to issue app.listen() on is being used by other programs". You can use a script like this to check if your port is in use and then change the port in your app.listen().

var net = require('net'); var errors = ['EADDRINUSE']; var isUsed = function(port) { var tester = net.createServer() .once('error', function (err) { if (!errors.includes(err.code)) { console.log("Port is in use, change the port."); } }) .once('listening', function() { tester.once('close', function() { console.log("You are good to go."); }) .close() }) .listen(port); } 

You can add other errors in the errors array to check for all sorts of error types as well.

Comments

0

Got this error when we accidentally had two local Express environments in the same instance pointing to the same port.

If you got this far down this list of answers, I hope this will be helpful and solve your problem.

Comments

0

In ZSH, when I typed exit, I noticed a message stating: zsh: you have suspended jobs.

  1. Type the word jobs, hit enter
  2. Type kill %1 (where %1 is the number of the job), hit enter
  3. Response should state terminated {job_name}

I found the answer here

Comments

0

Steps to resolve it -:

1: need to kill the process with following command.

pm2 kill <PROCESS_ID>

2: restart the service again with following command:

pm2 start app.js --name "servername"

3: check the status of the server with following command.

pm2 list

1 Comment

This worked for me though I don't see why.
0

Actually you are trying to run on a port thats actually taken up by some other services (usually other webservers like apache, iis etc)

  1. Figure out which service is using the port

  2. Stop it and restart node server

Or go for another port like 8080,5000...

For windows

netstat -ano -p tcp |find "80" 

For linux

 sudo lsof -i -P -n 

Comments

0

If you use docker image with nodejs just stop container:

  • to get nodejs CONTAINER_ID
docker ps 
  • and stop it
docker stop CONTAINER_ID 
  • or if you use docker compose
docker compose stop nodejs_service_name 

Comments

-2

It's also useful to look into what process is using the port.

For macOS I found out from this article that AirPlay uses Port 5000 so even if you kill it using the solutions listed here, it will take-up Port 5000 again. Do a quick Google search to see what common apps or processes are using the port that is causing the error.

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.