ExpressJS - throw er Unhandled error event

node.jsExpressNpm

node.js Problem Overview


I created expressjs application using the following commands:

express -e folderName
npm install ejs --save
npm install

When I run the application with: node app.js, I have the following errors:

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: listen EADDRINUSE
	at errnoException (net.js:884:11)
	at Server._listen2 (net.js:1022:14)
	at listen (net.js:1044:10)
	at Server.listen (net.js:1110:5)
	at Object.<anonymous> (folderName/app.js:33:24)
	at Module._compile (module.js:456:26)
	at Object.Module._extensions..js (module.js:474:10)
	at Module.load (module.js:356:32)
	at Function.Module._load (module.js:312:12)
	at Function.Module.runMain (module.js:497:10)

How to fix it?

node.js Solutions


Solution 1 - node.js

You had run another server use the same port like 8080.

Maybe you had run node app in other shell, Please close it and run again.

You can check PORT no. is available or not using

netstat -tulnp | grep <port no>

Alternatively, you can use lsof:

lsof -i :<port no>

Solution 2 - node.js

We do get similar error when we sometimes run our express app. We have to follow the same in that case. We need to check if its running in any terminal. If you want to find and kill process, follow these steps:

  • ps aux | grep node
  • Find the process ID (second from the left):
  • kill -9 PRCOCESS_ID

OR

Use a single command to close all the running node processes.

ps aux | awk '/node/{print $2}' | xargs kill -9

Solution 3 - node.js

An instance is probably still running. This will fix it.

killall node

Update: This command will only work on Linux/Ubuntu & Mac.

Solution 4 - node.js

If you're on Linux, this problem can also occur if Nodejs is not running as root.

Change from this:

nodejs /path/to/script.js

To this:

sudo nodejs /path/to/script.js

Just happened to me and none of the other suggestions here fixed it. Luckily I remembered the script was working the other day when running as root. Hope this helps someone!

Disclaimer: This probably isn't the best solution for a production environment. Starting your service as root may introduce some security holes to your server/application. In my case, this was a solution for a local service, but I'd encourage others to spend some more time trying to isolate the cause.

Solution 5 - node.js

This is because the port you are using to run the script is already in use. You have to stop all other nodes which are using that post. for that, you can check all node by

ps -e

OR for node process only use ps -ef | grep node This will give you the list of all node process with id

to Kill all node process

sudo killall -9 node

Or for the specific id sudo kill -9 id

Solution 6 - node.js

I fixed the bug by changing the port which was

app.set('port', process.env.PORT || 3000);<br>

and changed to:

app.set('port', process.env.PORT || 8080);<br>

Solution 7 - node.js

The port Node is trying to use can be already used by another program. In my case it was ntop, which I had recently installed. I had to open http://localhost:3000/ in a browser to realize it. Another way to find the process is given here.

Solution 8 - node.js

Reason for this error

> Some other process is already running on the port you have specified

Simple and Quick solution

On Linux OS, For example you have specified 3000 as the port

  • Open the terminal and run lsof -i :3000. If any process is already running on port 3000 then you will see this printing on the console

COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
node    16615 aegon   13u  IPv6 183768      0t0  TCP *:3000 (LISTEN)

  • Copy the PID (process ID) from the output

  • Run sudo kill -9 16615 (you have to put PID after -9)

  • Start the server again

Solution 9 - node.js

Close any other node servers that are running, even if they are in other terminal windows or running on different ports. That should fix the problem.

Solution 10 - node.js

If you want to use the same port number then type kill % in the terminal, which kills the current background process and frees up the port for further usage.

Solution 11 - node.js

this means your file is running now. just enter below code and try again:

sudo pkill node

Solution 12 - node.js

Actually Ctrl+C keys not releasing port used by node process. So there is this error. The resolution to the issue was using following code snippet in server.js:

process.on('SIGINT', function() {
  console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
  // some other closing procedures go here
  process.exit(1);
});

This worked for me.

You can also check for other solutions mentioned at Graceful shutdown in NodeJS

Solution 13 - node.js

If you've tried killing all node instances and other services listening on 3000 (the default used by the express skeleton setup) to no avail, you should check to make sure that your environment is not defining 'port' to be something unexpected. Otherwise, you'll likely get the same error. In the express skeleton's app.js file you'll notice line 15:

app.set('port', process.env.PORT || 3000);

Solution 14 - node.js

In-order to fix this, terminate or close the server you are running. If you are using Eclipse IDE, then follow this,

Run > Debug

enter image description here

Right-click the running process and click on Terminate.

Solution 15 - node.js

events.js:183 throw er; // Unhandled 'error' event

I also got the same kind of problem and tried many ways but finally got this, this works well:

npm install ws@3.3.2 --save-dev --save-exact

Refer to this link for more clarifications https://github.com/ionic-team/ionic-cli/issues/2922

Solution 16 - node.js

I ran into the same issue today and the port was not used. The following approach helped:

rm -rf node_modules && npm cache clean && npm install
npm start

Solution 17 - node.js

On windows :

cmd 1 : lsof -i :<port no>

This gives the process id

cmd 2 : kill -9 <process id>

done

Solution 18 - node.js

In my case I've had to run vagrant reload as well. Even with no node processes running my express app in my virtual machine I was still getting this error until reloading the vagrant box.

Solution 19 - node.js

Stop the service that is using that port.

sudo service NAMEOFSERVICE stop

Solution 20 - node.js

In my case the issue was caused by forgetting to call next() in an expressjs `use' method call.

> If the current middleware does not end the request-response cycle, it must call next() to pass control to the next middleware, otherwise the request will be left hanging.

http://expressjs.com/guide/using-middleware.html

Solution 21 - node.js

This worked for me.

http://www.codingdefined.com/2015/09/how-to-solve-nodejs-error-listen.html

Just change the port number from the Project properties.

Solution 22 - node.js

You can also change the port from Gruntfile.js and run again.

Solution 23 - node.js

After killing the same process multiple times and not being able to locate what else was running on port 8000, I realized I was trying to run on port 8000 twice:

Before:

MongoClient.connect(db.url, (err, database) => {
  if (err) return console.log(err);
  require('./app/routes')(app, database);

  app.listen(port, () => {
    console.log('We are live on ' + port);
  });
});

require('./app/routes')(app, {});
app.listen(port, () => {
  console.log("We are live on " + port);
});

After:

MongoClient.connect(db.url, (err, database) => {
  if (err) return console.log(err);
  require('./app/routes')(app, database);

  app.listen(port, () => {
    console.log('We are live on ' + port);
  });
});

require('./app/routes')(app, {});

Solution 24 - node.js

I had the same problem and I found out, that a nodejs process that I had previously canceled with CTRL+C was still running. The problem in Windows 10 is, that Ctrl + C Doesn't Kill Gracefully nodejs. I opened the task manager and killed the process manually. The solutions provided on GitHub didn't work for me.

Solution 25 - node.js

If you using windows, then you can end process from task manager for node.js

Solution 26 - node.js

None of the answers worked for me.

When I restarted my computer I could get the server up and running.

Mac
shutdown now -r

Linux
sudo shutdown now -r

Solution 27 - node.js

->check what’s running on port 8080 or what ever port u want to check

lsof -i @localhost:8080

if something is running u can close it or use some kill command to close it

Solution 28 - node.js

Simple just check your teminal in Visual Studio Code Because me was running my node app and i hibernate my laptop then next morning i turn my laptop on back to development of software. THen i run the again command nodemon app.js First waas running from night and the second was running my latest command so two command prompts are listening to same ports that's why you are getting this issue. Simple Close one termianl or all terminal then run your node app.js or nodemon app.js

Solution 29 - node.js

The port you are listening to is already being listened by another process.

When I faced to this error I killed the process using Windows PowerShell (because I used Windows)

  1. List itemopen the windows powershell
  2. type ps and then you can get list of processes
  3. find the process named node, and note the Id
  4. type Stop-process <Id> I think that it is help for windows users

Solution 30 - node.js

IF it is in mac then it's all about IP of x86_64-apple-darwin13.4.0. If you follow errors it would be something related to x86_64-apple-darwin13.4.0. Add

127.0.0.1 x86_64-apple-darwin13.4.0

to /etc/hosts file. Then the problem is gone

Solution 31 - node.js

My answers is one of the solution

If you are trying to run angular application with below command

ng serve --open

--open Opens the url in default browser this tag is causing the issue. i don't know what is the purpose after angular 10 version which is not working. I am still analysing

Solution 32 - node.js

Just change your port,might be your current port is in use by iis or some other server.

Solution 33 - node.js

Try the below fixes.

  1. Try to close the process that is using your port. > netstat -tulnp | grep <port_number>

  2. Installing the below pakcage fixed it for me forever. > npm install [email protected] --save-dev --save-exact

  3. Run this command in your terminal :

    > echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

    For Arch Linux add this line to /etc/sysctl.d/99-sysctl.conf:

    fs.inotify.max_user_watches=524288

    Then execute:

    sysctl --system

    This will also persist across reboots.

    https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers#the-technical-details

Solution 34 - node.js

npm install --save --save-exact [email protected].

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJR GaliaView Question on Stackoverflow
Solution 1 - node.jsGlowinView Answer on Stackoverflow
Solution 2 - node.jsmonicalView Answer on Stackoverflow
Solution 3 - node.jsTawabGView Answer on Stackoverflow
Solution 4 - node.jsCauselessEffectView Answer on Stackoverflow
Solution 5 - node.jsAbhinav bhardwajView Answer on Stackoverflow
Solution 6 - node.jsmarcdahanView Answer on Stackoverflow
Solution 7 - node.jsFabienView Answer on Stackoverflow
Solution 8 - node.jsHadi MirView Answer on Stackoverflow
Solution 9 - node.jsJake McGuireView Answer on Stackoverflow
Solution 10 - node.jsSuneha JavidView Answer on Stackoverflow
Solution 11 - node.jsAbolfazl MiadianView Answer on Stackoverflow
Solution 12 - node.jsPankaj ShindeView Answer on Stackoverflow
Solution 13 - node.jsMarkView Answer on Stackoverflow
Solution 14 - node.jsPrashanth SamsView Answer on Stackoverflow
Solution 15 - node.jsJanith UdaraView Answer on Stackoverflow
Solution 16 - node.jsseb_domView Answer on Stackoverflow
Solution 17 - node.jsShreyash JainView Answer on Stackoverflow
Solution 18 - node.jscbaigorriView Answer on Stackoverflow
Solution 19 - node.jsRickView Answer on Stackoverflow
Solution 20 - node.jsAndrew DwyerView Answer on Stackoverflow
Solution 21 - node.jsmukulsharma1146View Answer on Stackoverflow
Solution 22 - node.jsNarendra SolankiView Answer on Stackoverflow
Solution 23 - node.jsAndrew SouthardView Answer on Stackoverflow
Solution 24 - node.jsGregor WeichbrodtView Answer on Stackoverflow
Solution 25 - node.jsCodeRiderView Answer on Stackoverflow
Solution 26 - node.jstiagomenegazView Answer on Stackoverflow
Solution 27 - node.jsrabiaasifView Answer on Stackoverflow
Solution 28 - node.jszaibView Answer on Stackoverflow
Solution 29 - node.jsAnushView Answer on Stackoverflow
Solution 30 - node.jsShravanView Answer on Stackoverflow
Solution 31 - node.jssathishView Answer on Stackoverflow
Solution 32 - node.jsAkashView Answer on Stackoverflow
Solution 33 - node.jsNatesh bhatView Answer on Stackoverflow
Solution 34 - node.jsjayaprakash kusireddyView Answer on Stackoverflow