505

If I run a server with the port 80, and I try to use xmlHTTPrequest i get 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();
Rishabh
  • 3,312
  • 4
  • 42
  • 69
Danny Fox
  • 31,515
  • 27
  • 62
  • 90
  • 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? – Timothy Meade Mar 28 '12 at 00:34
  • See a similar issue at http://stackoverflow.com/questions/8553957/how-to-release-localhost-from-error-listen-eaddrinuse/31072560#31072560 – Manohar Reddy Poreddy Jun 26 '15 at 12:01
  • Which system are you on? Some systems require sudo if you want to listen to ports below a certain treshold. – Kebman Jul 30 '15 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) – Ashad Nasim Mar 10 '19 at 05:47
  • https://stackoverflow.com/questions/39632667/how-to-kill-the-process-currently-using-a-port-on-localhost-in-windows – Abhay Apr 01 '19 at 09:02

41 Answers41

595

What really helped for me was:

killall -9 node

But this will kill a system process.

With

ps ax

you can check if it worked.

Patrick
  • 6,023
  • 1
  • 11
  • 7
  • 1
    Also for me. In my case I just run the listen function twice and got the error in the second – vabada Apr 06 '15 at 07:16
  • 2
    On a related note you may also read [when should i not kil -9 a process](http://unix.stackexchange.com/questions/8916/when-should-i-not-kill-9-a-process). – Aurelio Aug 04 '15 at 10:00
  • 12
    The Issue here is you guys are not exiting the node process gracefully after the first run. Therefore, node is still binded to that port. `ps aux | grep node` would show that. Instead of killing the application with **CTRL+Z**, exit the application with **CTRL+C**. This exits the application gracefully and the port binding is removed. – riser101 Dec 07 '15 at 10:29
  • 26
    Over 150 up-votes for a solution that equates to hitting your software with a mallet. – Lee Goddard Mar 23 '16 at 11:14
  • May be hit with a mallet but when node gets an exception it may be the only solution worked for me in meteor. – Mahib Dec 22 '16 at 19:29
  • 2
    This solution is quite problematic since -9 flag will kill the process without releasing the memory. You should really use this only as a last solution. – Yaki Klein Jun 04 '17 at 06:23
  • Thank you! This solved the problem. It happened to me twice and it solved it! – Emanuela Colta Nov 18 '17 at 13:29
  • 1
    This answer is deeply problematic... partly because, as @LeeGee noted, it's a blunt tool where something more precise will surely do, but also because it encourages bad habits, of swinging that mallet around indiscriminately without thought. Much better to figure out what's running, think about _whether_ it should be killed, and then kill it _gracefully_ (with the default kill -15/SIGTERM) _if_ the situation warrants. Though from the sound of the question, I'm thinking they're just trying to run the server over and over, not realizing it's a _server_... that _keeps running_... _by design_. – lindes May 07 '19 at 01:10
  • For Windows users, open your task manager and end any Node processes. – Doğa Gençer Sep 23 '19 at 12:54
  • Thanks a lot for helping me out! – Travis Le Aug 24 '20 at 09:43
422

EADDRINUSE means that the port number which listen() tries to bind the server to is already in use.

So, 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);
Gabriel Theron
  • 1,296
  • 3
  • 20
  • 46
stewe
  • 39,054
  • 13
  • 75
  • 73
  • 1
    I run only this server. Befor I start the server, the xmlhttprequest works. After I start the server on the port 80, then the server also works perfectly. But if I do an xmlhttprequest after I started the server, then I get this error. – Danny Fox Mar 27 '12 at 22:54
  • 7
    Won't this still throw an error if the server is already listening? – trysis Jun 16 '15 at 16:27
  • 1
    For me this was caused by Skype – Beep Jun 21 '17 at 14:03
294

The aforementioned killall -9 node, suggested by Patrick works as expected and solves the problem but you may want to read the edit part of this very 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

This will return something like:

COMMAND   PID    USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node     57385   You   11u  IPv6 0xac745b2749fd2be3      0t0  TCP *:ddi-tcp-1 (LISTEN)

Then just do (ps - actually do not. Please keep reading below):

kill -9 57385

You can read a bit more about this here.

EDIT: I was reading on a fairly related topic today and stumbled upon this interesting thread on why should i not kill -9 a process.

Generally, you should use kill -15 before kill -9 to give the target process a chance to clean up after itself. (Processes can't catch or ignore SIGKILL, but they can and often do catch SIGTERM.) If you don't give the process a chance to finish what it's doing and clean up, it may leave corrupted files (or other state) around that it won't be able to understand once restarted.

So, as stated you should better kill the above process with:

kill -15 57385

EDIT 2: As noted in a comment around here many times this error is a consequence of not exiting a process gracefully. That means, a lot of people exit a node command (or any other) using CTRL+Z. The correct way of stopping a running process is issuing the CTRL+C command which performs a clean exit.

Exiting a process the right way will free up that port while shutting down. This will allow you to restart the process without going through the trouble of killing it yourself before being able to re-run it again.

Aurelio
  • 20,064
  • 8
  • 54
  • 61
62

Just a head's up, 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)

Turn off Skype port 80 usage

P.S. After making that change, don't forget to restart Skype!

SE_net4 the downvoter
  • 21,043
  • 11
  • 69
  • 107
Rob Evans
  • 6,066
  • 3
  • 35
  • 52
  • 14
    P.S. After making that change, don't forget to restart Skype! – Rob Evans Apr 12 '13 at 12:08
  • 18
    This is one of the most dumbfounding design flaws I've ever seen. How insane are the Skype devs that they would *ever* consider taking over 80 or 443? – AJB Sep 21 '15 at 21:35
  • 8
    Big +1 for your debugging skills though, Rob. – AJB Sep 21 '15 at 21:35
  • @AJB They did it to try and punch through firewalls that limit outbound traffic to http requests, but where the firewalls aren't using DPI so just do basic port blocking. Still... it's a bit silly to enable this by default! – Rob Evans Sep 22 '15 at 10:52
  • Yeah, that occurred to me after a bit of thought on why they would do this. Still, a really ugly kludge. And the idea that it's enabled by default is just plain arrogant. – AJB Sep 22 '15 at 18:45
44

You should try killing the process that is listening on port 80.

Killall will 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    
Yaki Klein
  • 2,584
  • 2
  • 30
  • 31
36

Error reason: You are trying to use the busy port number

Two possible solutions for Windows/Mac

  1. Free currently used port number
  2. Select another port number for your current program


1. Free Port Number

Windows

1. netstat -ano | findstr :4200
2. taskkill /PID 5824 /F

enter image description here

Mac

You can try netstat

netstat -vanp tcp | grep 3000

For OSX El Capitan and newer (or if your netstat doesn't support -p), use lsof

sudo lsof -i tcp:3000

if this does not resolve your problem, Mac users can refer to complete discussion about this issue Find (and kill) process locking port 3000 on Mac


2. Change Port Number?

Windows

set PORT=5000

Mac

export PORT=5000
WasiF
  • 15,431
  • 9
  • 81
  • 100
  • you can do in one command in windows too: `netstat -ona | findstr ".0:PORT +0.0.0.0:0 +LISTENING" | for /f "tokens=5" %t in ('more') do taskkill /PID:%t /f` – Bernardo Dal Corno Jun 15 '19 at 05:53
29

Under a controller env, you could use:

pkill node before running your script should do the job.

Bear in mind this command will kill all the node processes, which might be right if you have i.e a container running only one instance, our you have such env where you can guarantee that.

In any other scenario, I recommend using a command to kill a certain process id or name you found by looking for it programmatically. like if your process is called, node-server-1 you could do pkill node-server-1.

This resource might be useful to understand: https://www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/

Javier Cobos
  • 1,063
  • 10
  • 17
  • 2
    `pgrep node` before hand if you want to be a little careful and see what `node` processes are running – Josh.F Apr 27 '16 at 04:22
  • 1
    Totally!, I was talking in case it was a container or very controlled place for the process. – Javier Cobos May 01 '17 at 06:39
  • This is awesome answer I will say than others provided above. Everyone please upvote it. – Jitendra Pawar Apr 08 '19 at 14:17
  • @JavierCobos: if you were speaking for within a container or similar controlled space, please add that information to the answer itself. Many people are not in such environments, especially on a development machine, and this could have unintended consequences as-is. Downvoting, but happy to change the vote with proper clarification. – lindes May 07 '19 at 01:14
  • @lindes I think that anyone working with such things should understand that `pkill node` is going to kill all node instances. For a more complex architecture, we would obviously need a more detail solution. The solution I gave, was very high level and generic and its to be understood it's just for dev, or quite under control scenarios. But you are right maybe I should have specified a bit more :) – Javier Cobos May 08 '19 at 09:44
  • 1
    Many will... but as someone who spends significant chunks of time helping train junior developers, and even beyond that context, I can tell you that a great many people make use of answers on Stack Overflow without really comprehending what they're doing... they're just faced with a problem, and see something represented as a solution to what seems to be their problem, and they run with it. So... I personally desire the site to give good explanations and foster good general habits. So, that's where I'm coming from on it. I know it's not the only perspective, though. :) – lindes May 09 '19 at 06:46
  • @lindes, I just updated it, let me know if you think I should add some extra info :) – Javier Cobos May 09 '19 at 11:08
  • Thanks, @JavierCobos. That's definitely enough to remove the down-vote (done). :) I think for it to get an up-vote from me, the answer would need to say more about what's actually causing the problem, and otherwise give more context (not just how to kill a process, but whether and why you might want to)... but down-vote removed, and I thank you for improving the answer. – lindes May 09 '19 at 16:06
17

Your application is already running on that port 8080 . Use this code to kill the port and run your code again

sudo lsof -t -i tcp:8080 | xargs kill -9
ranjith
  • 349
  • 3
  • 9
  • The question references port 80, not 8080. Also, while this probably would work to get rid of an offending process on port 8080, using `kill -9` is almost certainly overkill, and, in my opinion, a horrible piece of advice to give without specific warnings about it. Just `kill` would probably do the trick, and really, the fundamental problem in this question is, I think, that they're trying to re-run a _server_ over and over again, so this is only a hack, not a fix. They need to have a better understanding of what's going on, which this answer does not really provide. – lindes May 07 '19 at 01:17
16

Another thing that can give this error, is two HTTP servers in the same node code. I was updating some Express 2 to Express 3 code, and had this...

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});        

// tons of shit.

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});                                                                   

And, it triggered this error.

user157251
  • 64,489
  • 38
  • 208
  • 350
16

This works for me (I'm using mac). Run this command

lsof -PiTCP -sTCP:LISTEN

This's going to display a list of ports that your syetem is using. Find the PID that your node is running

COMMAND     PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node      17269 hientrq   16u  IPv6 0xc42959c6fa30c3b9      0t0  TCP *:51524 (LISTEN)
node      17269 hientrq   19u  IPv4 0xc42959c71ae86fc1      0t0  TCP localhost:1337 (LISTEN)

and run kill -9 [YOUR_PID]

hientrq
  • 593
  • 5
  • 11
16
lsof -i:3000;
kill -9 $(lsof -t -i:3000);
// 3000 is a your port
// This "lsof -i:3000;" command will show PID 
kill PID 
ex: kill 129393
Pramod Jain
  • 404
  • 4
  • 5
  • 4
    Please add more context to your answers to help future readers understand the code/command. – Milo Jul 25 '19 at 18:41
11

EADDRINUSE means that the port(which we try to listen in node application) is already being used. In order to overcome, we need to identify which process is running with that port.

For example if we are trying to listen our node application in 3000 port. We need to check whether that port is already is being used by any other process.

step1:

$sudo netstat -plunt |grep :3000

That the above command gives below result.

tcp6       0      0 :::3000                 :::*                    LISTEN      25315/node

step2:

Now you got process ID(25315), Kill that process.

kill -9 25315

step3:

npm run start

Note: This solution for linux users.

Thavaprakash Swaminathan
  • 4,158
  • 1
  • 23
  • 22
8

sudo kill $(sudo lsof -t -i:80)

for force kill

sudo kill -9 $(sudo lsof -t -i:80)

use above cmd to kill particular port and then run your server

8

Try both commands and it will stop all node process.

killall 9 node
pkill node
npm start 
Anupam Maurya
  • 1,015
  • 13
  • 23
6

There is a way to terminate the process using Task Manager:

Note that this solution is for Windows only

  1. Go to the Task Manager (or using the shortcut Ctrl + Shift + Esc)

  2. On "Background Processes", find "Node.js" processes and terminate them (Right-click them and choose "End Task")

enter image description here

  1. Now you should be able to start again
Jee Mok
  • 4,537
  • 7
  • 35
  • 66
5

This error comes when you have any process running on a port on which you want to run your application.

how to get which process running on that port=> command: sudo netstat -ap | grep :3000

output : you will get the process information which is using that port

tcp 0 0 IPaddress:3000 : LISTEN 26869/node

Now you can kill that process sudo kill -9 26869

WAQAR SHAIKH
  • 141
  • 1
  • 6
5

In below command replace your portNumber

sudo lsof -t -i tcp:portNumber | xargs kill -9
Nick
  • 118,076
  • 20
  • 42
  • 73
5

EADDRINUSE means port of your nodejs app is already in use.

  • Now you have kill the process/app running on that port.
  • Find the process id of app by:

lsof -i tcp:3000

  • Now u will get process id from this.
  • Run this:

kill -9 processId

3

I have the same problem too,and I simply close the terminal and open a new terminal and run

node server.js

again. that works for me, some time just need to wait for a few second till it work again.

But this works only on a developer machine instead of a server console..

robinclark007
  • 145
  • 1
  • 1
  • 11
3

Error: listen EADDRINUSE means the port which you want to assign/bind to your application server is already in use. You can either assign another port to your application.

Or if you want to assign the same port to the app. Then kill the application that is running at your desired port.

For a node application what you can try is, find the process id for the node app by :

ps -aux | grep node

After getting the process id, do

kill process_id
Parth Vyas
  • 467
  • 4
  • 16
3

In my case Apache HTTP Server was run on port 80 I solved it by issue the command as root

sudo killall httpd

Update

If Jenkin is installed and running on your Mac;

  1. You can check it with sudo lsof -i tcp:8080
  2. If Yes, and You want to stop Jenkins only once, run: sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
Chotala Paresh
  • 400
  • 2
  • 11
3

I got:

Error: listen EADDRINUSE: address already in use :::8000

I was trying to look for the process listening to port 8000
and had no luck - there were none
(sudo netstat -nlp | grep 8000 ).

It turned out I had app.listen(8000) written twice in my script.

My assumption is that the interference was happening only in a short time when trying to run the script, so looking for processes listening to the port before and after error didn't show any.

BanAnanas
  • 317
  • 4
  • 13
  • Ok, weirdest thing: After this change made it work for 2-3 times rerunning the script, the error reappeared, but this time there was actually node process listening to the port and I resolve id by killing the process. It's been few hours now and no error. I'm positive that the first error was caused by what I mentioned (I'm able to recreate it), but if the second had anything to do with it or it was just pure coincidence remains a mystery to me. – BanAnanas May 22 '20 at 21:18
3

I have seen this error before (in node) with http.client, and as I recall, the problem had to do with not initializing the httpClient or setting bad options in the httpClient creation and/or in the url request.

ControlAltDel
  • 28,815
  • 6
  • 42
  • 68
2

On Debian i found out to run on port 80 you need to issue the command as root i.e

sudo node app.js

I hope it helps

Khurram Ijaz
  • 1,756
  • 5
  • 22
  • 42
2

For windows users execute the following command in PowerShell window to kill all the node processes.

Stop-Process -processname node
rawel
  • 2,565
  • 20
  • 30
2

I had the same issue recently.

It means that the port is already being used by another application (express or other software)

In my case, I had accidentally run express on 2 terminals, so exiting the terminal using 'Ctrl + C' fixed things for me. (Run server from only one terminal)

Hope it helps others.

Rahul Makhija
  • 231
  • 2
  • 7
2

Seems there is another Node ng serve process running. Check it by typing this in your console (Linux/Mac):

ps aux|grep node

and quit it with:

kill -9 <NodeProcessId>

OR alternativley use

ng serve --port <AnotherFreePortNumber>

to serve your project on a free port of you choice.

Birol Efe
  • 1,325
  • 12
  • 12
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
Nizar Blond
  • 1,666
  • 4
  • 16
  • 37
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.

Jorge Mejia
  • 1,073
  • 8
  • 27
1

Two servers can not listen on same port, so check out if other server listening on same port, also check out for browser sync if its running on same port

blackHawk
  • 4,966
  • 8
  • 47
  • 94
1

For other people on windows 10 with node as localhost and running on a port like 3500, not 80 ...

What does not work:

killall    ?  command not found
ps -aux | grep 'node'     ?     ps:  user x unknown 

What shows information but still not does work:

 ps -aef | grep 'node'
 ps ax
 kill -9 61864

What does work:

Git Bash or Powershell on Windows

  net -a -o | grep 3500   (whatever port you are looking for) 

Notice the PID ( far right )
I could not get killall to work... so

  1. Open your task manager
  2. On processes tab , right click on Name or any column and select to include PID
  3. Sort by PID, then right click on right PID and click end task.

Now after that not so fun exercise on windows, I realized I can use task manager and find the Node engine and just end it.

FYI , I was using Visual Studio Code to run Node on port 3500, and I use Git Bash shell inside VS code. I had exited gracefully with Ctrl + C , but sometimes this does not kill it. I don't want to change my port or reboot so this worked. Hopefully it helps others. Otherwise it is documentation for myself.

xlm
  • 4,594
  • 13
  • 42
  • 47
Tom Stickel
  • 16,699
  • 6
  • 102
  • 108
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
prakash tank
  • 1,249
  • 9
  • 15
1

The error EADDRINUSE (Address already in use) reports that there is already another process on the local system occupying that address / port.

There is a npm package called find-process which helps finding (and closing) the occupying process.

Here is a little demo code:

const find = require('find-process')

const PORT = 80

find('port', PORT)
.then((list) => {
  console.log(`Port "${PORT}" is blocked. Killing blocking applications...`)
  const processIds = list.map((item) => item.pid)
  processIds.forEach((pid) => process.kill(pid, 10))
})

I prepared a small sample which can reproduce the EADDRINUSE error. If you launch the following program in two separate terminals, you will see that the first terminal will start a server (on port "3000") and the second terminal will close the already running server (because it blocks the execution of the second terminal, EADDRINUSE):

Minimal Working Example:

const find = require('find-process')
const http = require('http')

const PORT = 3000

// Handling exceptions
process.on('uncaughtException', (error) => {
  if (error.code === 'EADDRINUSE') {
    find('port', PORT)
      .then((list) => {
        const blockingApplication = list[0]
        if (blockingApplication) {
          console.log(`Port "${PORT}" is blocked by "${blockingApplication.name}".`)
          console.log('Shutting down blocking application...')
          process.kill(blockingApplication.pid)
          // TODO: Restart server
        }
      })
  }
})

// Starting server
const server = http.createServer((request, response) => {
  response.writeHead(200, {'Content-Type': 'text/plain'})
  response.write('Hello World!')
  response.end()
})

server.listen(PORT, () => console.log(`Server running on port "${PORT}"...`))
Benny Neugebauer
  • 40,817
  • 21
  • 196
  • 177
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

SMshrimant
  • 483
  • 7
  • 14
  • 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. – lindes May 07 '19 at 01:26
  • Ok, If it worked for you. You can upvote this. :) @lindes – SMshrimant Jul 01 '19 at 09:24
  • 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. :) – lindes Jul 13 '19 at 00:53
1

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

cocomatt
  • 93
  • 1
  • 1
  • 10
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

MEAbid
  • 510
  • 7
  • 9
0

Windows is always tricky with open source..

change the port simply it works

node-inspector --web-port=8099
Ignatius Andrew
  • 6,788
  • 2
  • 43
  • 48
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.

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.

Michael Davidson
  • 405
  • 6
  • 15
0

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.

Bruce Seymour
  • 1,221
  • 11
  • 21
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

Adam Gonzales
  • 643
  • 6
  • 10