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

39,179

Solution 1

--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');
});

Solution 2

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.

Solution 3

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.

Solution 4

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)

Solution 5

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

npx kill-port 5000
Share:
39,179

Related videos on Youtube

Rishav Pandey
Author by

Rishav Pandey

I'm Rishav Pandey — a Full Stack Web and Native App Developer, Competitive Programmer, Data Science Enthusiast, and an Open Source Contributor, who's in love with </>.

Updated on July 09, 2022

Comments

  • Rishav Pandey
    Rishav Pandey over 1 year

    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)

    • Edison Augusthy
      Edison Augusthy over 3 years
      Please try changing port from 5000 to some other like 5555
    • Rishav Pandey
      Rishav Pandey over 3 years
      I tried this, and it has worked sometimes. But I'm not still getting why this error is occuring
    • Andrés Muñoz
      Andrés Muñoz over 3 years
      Could you please provide the .env file used by dotenv?
    • Rishav Pandey
      Rishav Pandey over 3 years
      @andresmunozit I'm only storing secret key in .env file
    • akkhil
      akkhil over 3 years
      This most likely happens because nodemon restarts the server faster than the KILL signal.
    • D. Geren
      D. Geren about 3 years
      From my experience this error, @akkhil 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.
  • Rishav Pandey
    Rishav Pandey over 3 years
    not it's not running, whenever I start nodemon, and then saving the file the error occurs
  • Rishav Pandey
    Rishav Pandey over 3 years
    Also, I only run front end server on port 3000, other than nothing is running on 5000. I've checked it several times
  • Fide
    Fide over 3 years
    Before running new instance of Node, check your opened ports and services: sudo ss -tulpn | grap LISTEN
  • BanAnanas
    BanAnanas over 3 years
    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.
  • Burak Karakuş
    Burak Karakuş over 3 years
    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.
  • ZADorkMan
    ZADorkMan about 3 years
    In my case, I forgot to stop debugging which was using the port. Thanks!
  • NightFury
    NightFury almost 3 years
    Thank you. That was a stupid mistake I made !!
  • mac
    mac over 2 years
    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...
  • Logemann
    Logemann over 2 years
    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.
  • amit thakur
    amit thakur over 2 years
    Not all superheroes wear cape!
  • g.delgado
    g.delgado over 2 years
    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.
  • Siddharth Sathe
    Siddharth Sathe over 2 years
    you are a genius
  • anarchist912
    anarchist912 over 2 years
    That exactly did it for me! Tried many other things..
  • Ballon Ura
    Ballon Ura over 2 years
    That exactly did it for me also! Tried many other things..
  • Arrow
    Arrow about 2 years
    Worked like charm!
  • Polem
    Polem almost 2 years
    This should not be an answer. Please delete it.
  • Dashiell Rose Bark-Huss
    Dashiell Rose Bark-Huss almost 2 years
    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.
  • anurag1905
    anurag1905 almost 2 years
    Worked for me. Thanks a lot!
  • David
    David over 1 year
    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.
  • redcartel
    redcartel over 1 year
    I am still experiencing this in 2.0.15
  • Vivek Kumar
    Vivek Kumar over 1 year
    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?'.