Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 652
Rishav Pandey Avatar asked Apr 13 '20 03:04

Rishav Pandey


People also ask

How do I stop an address that is already in use?

The Error “address already in use” occurred because some process was already running on the same port. So we can resolve the issue just by killing the process. To stop the process, we need the process ID (PID), which we can fetch using the lsof command.

What is Nodemon JSON?

nodemon supports local and global configuration files. These are named nodemon. json and can be located in the current working directory or in your home directory. The specificity is as follows, so that a command line argument will always override the config file settings: command line arguments.

How do I fix the “eaddrinuse address already in use” error?

Mostly in development machines, we tend to leave out the node server running, and close the terminal. This keeps the node server running, and when we try to restart the application, it throws the “EADDRINUSE: address already in use” error. This can be fixed by first finding out which PID the process is using and then kill it.

Does nodemon work without ESM?

it works without esm, i.e. if I change "dev": "nodemon -r esm app" to "dev": "nodemon app", the error goes away, and nodemon restarts correctly it works either way without express or any web server (because the port is not used)

How to terminate eaddrinuse eaddrinuse error?

To terminate, run You can check if the port has got free by running the first command again, and you should get no results, making sure that the EADDRINUSE EADDRINUSE error is resolved. To be able to use multiple node versions on the system, we can use NVM (Node Version Manager).

Why is nodemon restarting faster than the process is killed?

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.


6 Answers

--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');
});
like image 156
Jeyhun Ashurbayov Avatar answered Oct 09 '22 07:10

Jeyhun Ashurbayov


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.

like image 44
Crystal Maiden Avatar answered Oct 09 '22 08:10

Crystal Maiden


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.

like image 20
Field Of Copper Avatar answered Oct 09 '22 07:10

Field Of Copper


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)
like image 12
Niraj Patel Avatar answered Oct 09 '22 07:10

Niraj Patel


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

npx kill-port 5000
like image 9
Nditah Avatar answered Oct 09 '22 07:10

Nditah


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.

like image 8
Donghao Wu Avatar answered Oct 09 '22 08:10

Donghao Wu