190
votes

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?

30
EADDRINUSE means that the port is already in use. try changing what port the web server in app.js listens on or kill whatever is currently using that port if you don't need it.go-oleg
If closing port is not fixing , try this > stackoverflow.com/a/52441297/6665568Natesh bhat

30 Answers

413
votes

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

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

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

killall node

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

16
votes

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.

13
votes

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

8
votes

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

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.

2
votes

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.

2
votes

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.

2
votes

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

sudo pkill node
2
votes

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

2
votes

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

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

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.

1
votes

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

1
votes

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

On windows :

cmd 1 : lsof -i :<port no>

This gives the process id

cmd 2 : kill -9 <process id>

done

0
votes

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.

0
votes

Stop the service that is using that port.

sudo service NAMEOFSERVICE stop
0
votes

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

0
votes

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.

0
votes

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

0
votes

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

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.

0
votes

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

0
votes

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

0
votes

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

0
votes

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

0
votes

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

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