42

I'm creating a project and using nodejs, express for the backend. Everything works fine but as I make any change in the file, nodemon is unable to restart the server due to following error:

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

index.js:

const express = require("express");
const morgan = require("morgan");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const FileStore = require("session-file-store")(session);
const dotenv = require("dotenv");
var passport = require("passport");

dotenv.config();

const PORT = process.env.PORT || 5000;

const app = express();

.....

app.listen(PORT, () => console.log(`Server listening on port ${PORT}!`));

package.json

{
  "name": "chat-app-backend",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon --ignore 'sessions/' index.js"
  },
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "cookie-parser": "^1.4.5",
    "cors": "^2.8.5",
    "debug": "~2.6.9",
    "dotenv": "^8.2.0",
    "express": "~4.16.0",
    "express-session": "^1.17.0",
    "http-errors": "~1.6.2",
    "jade": "~1.11.0",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.9.4",
    "morgan": "~1.9.0",
    "passport": "^0.4.1",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "passport-local-mongoose": "^6.0.1",
    "session-file-store": "^1.4.0",
    "uuid": "^7.0.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.2"
  }
}

I've to explicitly kill the server from the terminal every time, which is not the optimal solution. I tried several things, but none are working. Even found some issue in nodemon GitHub issue page, but there also I couldn't find anything.

I'm also adding the output of lsof -i:5000, even if server I'm closing the server - *node 31625 rishav 20u IPv6 5300049 0t0 TCP :5000 (LISTEN)

RobC
  • 22,977
  • 20
  • 73
  • 80
Rishav Pandey
  • 664
  • 1
  • 6
  • 18

31 Answers31

51

--delay helped me to fix both of issues

  • on auto-restart

  • stopping with ctrl-c

    nodemon --delay 500ms app.js
    

And I also added:

process.once('SIGUSR2', function () {
  process.kill(process.pid, 'SIGUSR2');
});

process.on('SIGINT', function () {
  // this is only called on ctrl+c, not restart
  process.kill(process.pid, 'SIGINT');
});
28

I had the same situation. If you are using Visual Studio Code, check your terminals. You might have other instances of the terminal that is already running your node server.

Crystal Maiden
  • 289
  • 3
  • 3
22

I think that error happens to me when I shut down the server's terminal without turning off nodemon beforehand.

Then, usually the next day, nodemon starts acting weird.

How I solved it: What I noticed is that when running ps -fp $(grep -u my_username), I got several instances of nodemon up and running. So I did pkill -f nodemon, which killed all nodemon instances, and then relaunched nodemon and all was good again in the wonderful realm of my Linux server.

Field Of Copper
  • 350
  • 2
  • 8
  • 3
    As incolas mentioned, running `pkill -f nodemon` helped me. I had been trying to simply kill the port alone, but that only led to a temporary fix. The problem would return within a few saves... – mac Feb 13 '21 at 23:29
  • Worked for me, as well. Any idea why that happened? – Leonardo Nobre Ghiggino Nov 03 '22 at 10:43
15

You can try running your server on some other port like 3000.

If you still want to use the same port, then you can use the following command to get the list of running process on that particular port:

lsof -i tcp:3000 

Use following command in the terminal to kill that running port:

sudo kill -9 $(lsof -i tcp:3000 -t)
Bennet
  • 33
  • 1
  • 5
Niraj Patel
  • 810
  • 8
  • 15
10

None of all the aforementioned solutions worked for me. I simply had to do this every time:

npx kill-port 5000
Nditah
  • 1,429
  • 19
  • 23
9

I had the same problem, every time when I saved files the error occurred.

** Updated on 3/18/2021 **

  • The fastest and direct way for me to solve this problem is by restarting the computer.
  • I am sure nodemon is still running somewhere even I re-open VS editor, which makes that address conflict.
  • This problem happens few times in my case.
  • I am still trying to figure out the root of the problem. This is not a perfect solution.

** End **

I need to exit the nodemon mode, find out the PID, and kill it. This is not a good solution because it is troublesome and it blocks me from getting the debugging logs in the terminal.

But I found another way to fix the problem, just change the file name from server.js to another name (for example, I tried server1.js), also change the script in the package.json file.("server": npx nodemon server1.js), finally run npm run server.

Before: enter image description here

Here is what I did:

  1. Change the file name from server.js to server1.js.
  2. Change the script in the package.json file, from "server": "npx nodemon server.js" to "server": "npx nodemon server1.js".
  3. $ npm run server.

After: enter image description here

I know this is a weird solution, maybe something is wrong with my computer or the nodemon package, but eventually, it does work for me, the file name does matter.

Here is an interesting observation I found on November 1, 2020:

  1. If you have both files, server.js and server1.js, and they have the same port number (8000), when npm run server, the error occurs.
  2. It seems that nodemon will run server.js automatically by default. Even though I only have "server": "npx nodemon server1.js" in package.json, when my terminal executes npm run server command, nodemon runs server.js and server1.js at the same time.
  3. Since server.js and server1.js have the same port number, that makes the error occurred.
  4. In a similar way, if we only have server.js, nodemon runs server.js automatically, then runs server.js according to package.json script, the error will occur.

Hope this will help. Please feel free to let me know if this works for your applications.

Donghao Wu
  • 99
  • 1
  • 3
6

All answers suggest a separate from nodemon fix for some reason. For the server to be correctly restarted you need to shut it down (terminate) with the proper signal. You can specify the signal with the --signal flag. For example:

nodemon --exec go run main.go --signal SIGTERM

In your particular example this would be:

nodemon --ignore 'sessions/' index.js --signal SIGTERM

More under Gracefully reloading down your script.

Georgy
  • 2,410
  • 3
  • 21
  • 35
6

A new possible reason is because in macOS Monterey (12.0), "AirPlay Receiver", which uses port 5000, and is denoted by process name ControlCenter is turned on by default. You can turn it off like this:

  1. Open System Preferences
  2. Go into "Sharing"
  3. Uncheck "AirPlay Receiver" at the bottom of the list on the left.

More details here.

You can also run your server on a different port, e.g., 3000.

5

Edit the code section where you declare the app as shown below.

    app.listen(PORT, function () {
    console.log(`The SERVER HAS STARTED ON PORT: ${PORT}`);
  })
  //   Fix the Error EADDRINUSE
  .on("error", function (err) {
    process.once("SIGUSR2", function () {
      process.kill(process.pid, "SIGUSR2");
    });
    process.on("SIGINT", function () {
      // this is only called on ctrl+c, not restart
      process.kill(process.pid, "SIGINT");
    });
  });

Eazy Pizzy. This worked for me. Thanks to : https://www.youtube.com/watch?v=YwP3KJ0lH_k

4
sudo pkill node

It will terminate all the running server

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
  • 478
  • 8
  • 17
3

How I solved it, since I was not running anything on that port and it still gave me the error. Kill node to clear is cache with:

pkill nodejs or pkill node depending on OS/version

Could also use fuser -k 8000/tcp to kill any port immediately. (linux)

Gabriel Petersson
  • 8,434
  • 4
  • 32
  • 41
3

It's possible that nodemon is restarting faster than the process is killed. This can happen if, for example, there is too slow code being executed on server.on('close') or server.on('SIGINT').

My solution is rather unsatisfying as I have no idea why it works, but it did for me. I declared the port number before the nodemon script, in package.json, and removed it from the PORT declaration in server.js:

try replacing:

const PORT = process.env.PORT || 5000;

by

const PORT = process.env.PORT;

and

"server": "nodemon server"

by

"server": "PORT=5000 nodemon server"
Tiago
  • 1,794
  • 1
  • 9
  • 16
2

If you are working on a file > saved it > then got the errr try this:

ReSave the file you made changes on once or twice more and the problem goes away for me every time.

Save the file once:

Error: listen EADDRINUSE: address already in use :::5000
...
[nodemon] app crashed - waiting for file changes before starting...

Save the file a seconds time (without making changes) [command]+ s

[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
Server is running on port: 5000

enter image description here

Michael Nelles
  • 5,426
  • 8
  • 41
  • 57
2

I'm experiencing the same with vscode. Rapid "double save" cmd-c works most of times.

arturasmckwcz
  • 104
  • 1
  • 7
2

I used this to fix the same problem

process.once('SIGUSR2', 
  function () { 
    process.kill(process.pid, 'SIGUSR2'); 
  }
);

https://www.npmjs.com/package/nodemon Controlling shutdown of your script

abdelhak.ajbouni
  • 170
  • 1
  • 10
2

As reported on github there was an issue related to this identified in nodemon@2.0.13. I am using version 2.0.12 and facing the same problem. A fix was implemented in 2.0.14 and upgrading to this version corrected the behavior.

Besworks
  • 4,123
  • 1
  • 18
  • 34
Vivek Kumar
  • 113
  • 2
  • 4
  • I am still experiencing this in 2.0.15 – redcartel Apr 14 '22 at 15:57
  • Try clearing the *npm cache* by running `npm cache clean`. You might want to add `--force` flag. If that still doesn't work, delete `node_modules` and `package-lock.json` and install `node_modules` again by running `npm install`. [Here](https://coder-coder.com/npm-clear-cache/#:~:text=Run%3A%20%E2%80%9Cnpm%20cache%20clean%20%E2%80%93force%E2%80%9D&text=are%20both%20not%20working%20and,npm%20cache%20on%20your%20computer.) is a good article on 'How to clear *npm cache*?'. – Vivek Kumar Apr 15 '22 at 04:35
  • Am using 2.0.20 and still face this – Daniyal Nasir Oct 22 '22 at 21:28
  • @DaniyalNasir did you try cleaning the npm cache? – Vivek Kumar Oct 23 '22 at 09:50
  • @VivekKumar Yes, several time but it still fails to close the previous process before restarting every now and then. – Daniyal Nasir Oct 23 '22 at 13:08
  • @DaniyalNasir perhaps try deleting `node_modules` and `package-lock.json` and install the packages again. – Vivek Kumar Oct 25 '22 at 03:24
  • @VivekKumar Did that too, when I was cleaning npm cache – Daniyal Nasir Oct 27 '22 at 09:20
1

Your port is used somewhere else. Most likely, you forgot to stop previous instance of node before running new one.

Fide
  • 1,127
  • 8
  • 7
  • 2
    not it's not running, whenever I start nodemon, and then saving the file the error occurs – Rishav Pandey Apr 13 '20 at 07:15
  • 1
    Also, I only run front end server on port 3000, other than nothing is running on 5000. I've checked it several times – Rishav Pandey Apr 13 '20 at 07:16
  • Before running new instance of Node, check your opened ports and services: `sudo ss -tulpn | grap LISTEN` – Fide Apr 13 '20 at 12:36
  • I had the situation where there seemed to be no other processes listening to the port, I posted my solution as an answer. It only turned out I had `app.listen(8000)` written twice in my script. – BanAnanas May 22 '20 at 20:06
1

If you want to use the port that is already running, you can simply kill it by using this command:

kill -9 $(lsof -t -i:portnumber)

In your case, if port number=5000, then you can run:

kill -9 $(lsof -t -i:5000)

ikhsanudinhakim
  • 1,554
  • 16
  • 23
1

I came across this recently in vscode working on node.js project using port 3000. At the terminal simply type:

npx kill-port 3000 
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
TLCW
  • 33
  • 5
0

Please try to upgrade your Node Version. It is worked for me.

  • The server cant bind the port which most likely means that there is some other process running on that port. It has surely nothing to do with a a node version. – Logemann Feb 22 '21 at 18:50
0

I was having the same issue when I had this script "start": "nodemon app.js" but then I changed script to this "server" : "npx nodemon app.js"

Which fixed that issue.

rickster
  • 174
  • 1
  • 2
  • 11
0

You can try using --spawn flag or spawn:true setting, this will use spawn instead of fork. This solution has one drawback: nodemon will leave zombie child when killed with SIGKILL, however it will still terminate zombie process on the next run.

nodemon --spawn --ignore 'sessions/' index.js

https://github.com/remy/nodemon/pull/1249

Krzysiek
  • 2,494
  • 16
  • 20
0

I had this error, I kept killing the process running on the required port using,

$ sudo kill -9 $(lsof -i tcp:<port number> -t}

every time I restart the server,but this removes the main use of nodemon. Then I listed all the processes running on my computer using

$ ps aux

then searched for all node application running on my computer,then found out my node app running in the background and then killed it using it's pid.

$ sudo kill -9 pid

this solved the error for me. The error occured because I lost connection to my server in between and then when I logged back in a new terminal was opened.

0

Maybe some other services is running on that port. You can try this if nothing works for you.

fuser -k [PORT]/tcp

For example

fuser -k 8080/tcp

0

I've seen good responses and they seem to fix.

I had the same problem and I hope my answer helps someone like me, I had not realized the following:

Looking at the package.json file I clicked on the "Debug" link above

"scripts:" {
"dev": "nodemon src / app.js"
}

and the console was automatically opened in visual studio code and I did not remember that the server was already running from the operating system console.

That was my mistake.

0

try to npm i nodemon@debug if it global use -g option if it work so support this PR : https://github.com/remy/nodemon/pull/1938

0

For me the issue was two different nodemon process, while working on my app, my vs code crashed, then I restarted the same and ran nodemon again. looks like there were two processes of nodemon running which was causing this issue.

Kapil Gupta
  • 332
  • 2
  • 8
0

deleting and reinstalling all node_modules and running npm cache clean --force worked for me.

Simone Anthony
  • 524
  • 6
  • 15
0

I have tried most of these solutions, but I founded that the best way is restarting your computer.

bardala
  • 51
  • 7
  • It's more than likely that at least one of these solutions worked but you only noticed after a reboot. The reboot itself is unlikely to be the cure. – Eric Aya Jul 19 '23 at 13:44
  • You may be correct, but I removed all modifications before the reboot. That is just what has happened with me. – bardala Jul 24 '23 at 23:47
  • Interesting, and unusual. If you add this info to your answer I will remove my comment. This might help others. – Eric Aya Jul 25 '23 at 07:13
  • it has worked well but now returns to the issue again :( – bardala Aug 14 '23 at 00:36
-3

My solution is simple

  1. suppose ex:- current port is 4000 then change it to 4001.

trust me it will work don't look for a big solution. if the problem with the existing port number change that to different as long as your application works no problem