JR Galia
JR Galia

Reputation: 17269

ExpressJS - throw er Unhandled error event

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?

Upvotes: 200

Views: 379056

Answers (30)

Shreyash Jain
Shreyash Jain

Reputation: 120

On windows :

cmd 1 : lsof -i :<port no>

This gives the process id

cmd 2 : kill -9 <process id>

done

Upvotes: 0

sathish
sathish

Reputation: 359

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

Upvotes: 0

Glowin
Glowin

Reputation: 5006

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>

Upvotes: 424

Shravan
Shravan

Reputation: 11

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

Upvotes: 0

seb_dom
seb_dom

Reputation: 113

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

Upvotes: 1

Anush
Anush

Reputation: 191

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

Upvotes: 0

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

Upvotes: -1

Hadi Mir
Hadi Mir

Reputation: 5133

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

Upvotes: 3

zaib
zaib

Reputation: 69

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

Upvotes: 0

rabiaasif
rabiaasif

Reputation: 312

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

Upvotes: 0

Pankaj Shinde
Pankaj Shinde

Reputation: 3689

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

Upvotes: 2

marcdahan
marcdahan

Reputation: 3062

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>

Upvotes: 9

CauselessEffect
CauselessEffect

Reputation: 413

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.

Upvotes: 15

tiagomenegaz
tiagomenegaz

Reputation: 161

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

Upvotes: 0

Abhinav bhardwaj
Abhinav bhardwaj

Reputation: 2737

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

Upvotes: 13

Janith Udara
Janith Udara

Reputation: 685

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 [email protected] --save-dev --save-exact

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

Upvotes: 1

Natesh bhat
Natesh bhat

Reputation: 13222

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

Upvotes: -1

CodeRider
CodeRider

Reputation: 582

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

Upvotes: 0

TawabG
TawabG

Reputation: 528

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

killall node

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

Upvotes: 37

Abolfazl Miadian
Abolfazl Miadian

Reputation: 3159

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

sudo pkill node

Upvotes: 2

Gregor Weichbrodt
Gregor Weichbrodt

Reputation: 121

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.

Upvotes: 0

Andrew Southard
Andrew Southard

Reputation: 61

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, {});

Upvotes: 0

Narendra Solanki
Narendra Solanki

Reputation: 1202

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

Upvotes: 0

Suneha Javid
Suneha Javid

Reputation: 37

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.

Upvotes: 2

Akash
Akash

Reputation: 1

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

Upvotes: -1

mukulsharma1146
mukulsharma1146

Reputation: 171

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.

Upvotes: 0

monical
monical

Reputation: 866

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

Upvotes: 66

Fabien
Fabien

Reputation: 7029

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.

Upvotes: 3

Andrew Dwyer
Andrew Dwyer

Reputation: 446

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

Upvotes: 0

Rick
Rick

Reputation: 13500

Stop the service that is using that port.

sudo service NAMEOFSERVICE stop

Upvotes: 0

Related Questions