Nodemon: Error: listen EADDRINUSE: address already in use :::5000
Asked Answered
J

31

44

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)

Jaunita answered 13/4, 2020 at 3:53 Comment(7)
Please try changing port from 5000 to some other like 5555Carrnan
I tried this, and it has worked sometimes. But I'm not still getting why this error is occuringJaunita
Could you please provide the .env file used by dotenv?Kettie
@andresmunozit I'm only storing secret key in .env fileJaunita
This most likely happens because nodemon restarts the server faster than the KILL signal.Blench
From my experience this error, @Blench seems to be the most correct. I save, restarting the server with nodemon. I sometimes get the same error. Without making changes, I save a file again, it restarts and I might get the error. It can take several saves/restarts before it works as it should.Xenos
I have same error. If your using VS code on Windwos then ccheck your terminal and close all terminal history ande then restart it.Caseinogen
S
55

--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');
});
Symbology answered 17/2, 2021 at 10:9 Comment(3)
This is the solution. Others have hinted that nodemon restarts faster than the KILL signal but didn't provide a solution. I have nodemon.json and just added nodemon --delay 500ms.Punak
if you have missed ms, it would wait for 500 secondsEncampment
solved the issue for me as well - thought I was going crazy! added "{"delay": "2500"} to nodemon.jsonProlate
L
33

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.

Lawful answered 1/7, 2020 at 20:37 Comment(4)
This is the exact answer that OP needs because that was the same case for me! Thank you, I forgot to check other terminal instances.Cannes
In my case, I forgot to stop debugging which was using the port. Thanks!Scandinavia
I quit VSC and it works now. But I believe it was because there was some other terminal instance that had opened and then I closed it but never actually killed the instance. So even if you don't see the terminal there might be another instance affecting it.Hydatid
this is the reason , there's a list of terminal instances in the right side cornerJilolo
N
23

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.

Neurocoele answered 4/2, 2021 at 11:43 Comment(2)
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...Hesse
Worked for me, as well. Any idea why that happened?Pyroelectricity
S
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)
Swap answered 13/4, 2020 at 6:11 Comment(0)
A
10

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

npx kill-port 5000
Antithesis answered 29/4, 2021 at 15:9 Comment(0)
T
8

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.

Trickery answered 1/11, 2020 at 4:53 Comment(1)
The "fastest and direct way to solve this problem" is not by restarting the computer. You've totally circled around the root of the issue and have done a bandaid fix.Constitution
G
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.

Gonad answered 30/4, 2021 at 1:25 Comment(0)
M
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.

Marvin answered 13/11, 2021 at 19:11 Comment(0)
T
6

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

Thurston answered 20/2, 2022 at 17:37 Comment(0)
L
5
sudo pkill node

It will terminate all the running server

Liturgist answered 16/8, 2021 at 19:41 Comment(0)
H
4

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"
Harney answered 11/4, 2021 at 0:52 Comment(0)
C
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.

Criswell answered 22/5, 2020 at 19:46 Comment(1)
Thank you. That was a stupid mistake I made !!Huskamp
R
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)

Rail answered 21/11, 2020 at 12:12 Comment(0)
R
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

Rhizoid answered 3/12, 2020 at 17:57 Comment(0)
B
2

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

Brittnee answered 16/12, 2020 at 20:26 Comment(0)
R
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

Rein answered 24/1, 2021 at 12:17 Comment(0)
S
2

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

npx kill-port 3000 
Surovy answered 6/2, 2022 at 18:6 Comment(0)
A
2

As reported on github there was an issue related to this identified in [email protected]. 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.

Asiatic answered 4/4, 2022 at 6:44 Comment(7)
I am still experiencing this in 2.0.15Bilski
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 is a good article on 'How to clear npm cache?'.Asiatic
Am using 2.0.20 and still face thisOfficiate
@DaniyalNasir did you try cleaning the npm cache?Asiatic
@VivekKumar Yes, several time but it still fails to close the previous process before restarting every now and then.Officiate
@DaniyalNasir perhaps try deleting node_modules and package-lock.json and install the packages again.Asiatic
@VivekKumar Did that too, when I was cleaning npm cacheOfficiate
S
1

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

Superfetation answered 13/4, 2020 at 6:0 Comment(4)
not it's not running, whenever I start nodemon, and then saving the file the error occursJaunita
Also, I only run front end server on port 3000, other than nothing is running on 5000. I've checked it several timesJaunita
Before running new instance of Node, check your opened ports and services: sudo ss -tulpn | grap LISTENSuperfetation
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.Criswell
K
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)

Kilburn answered 22/1, 2022 at 22:40 Comment(0)
T
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.

Trouper answered 19/6, 2021 at 6:53 Comment(0)
T
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

Trabzon answered 19/6, 2021 at 21:58 Comment(0)
L
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.

Larva answered 12/7, 2021 at 11:23 Comment(0)
B
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

Bunghole answered 27/7, 2021 at 13:15 Comment(0)
C
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.

Cabbala answered 3/9, 2021 at 20:41 Comment(0)
B
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

Barrettbarrette answered 18/10, 2021 at 12:2 Comment(0)
W
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.

Woll answered 17/3, 2022 at 7:10 Comment(0)
Z
0

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

Zenaidazenana answered 9/10, 2022 at 2:48 Comment(0)
D
0

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

Duala answered 19/7, 2023 at 13:25 Comment(4)
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.Bogus
You may be correct, but I removed all modifications before the reboot. That is just what has happened with me.Duala
Interesting, and unusual. If you add this info to your answer I will remove my comment. This might help others.Bogus
it has worked well but now returns to the issue again :(Duala
A
-1

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

Abm answered 22/2, 2021 at 13:3 Comment(1)
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.Ducat
B
-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

Balfour answered 12/2, 2021 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.