Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

[nodemon] Error: listen EADDRINUSE: address already in use #676

Closed
alex996 opened this issue Nov 25, 2018 · 14 comments
Closed

[nodemon] Error: listen EADDRINUSE: address already in use #676

alex996 opened this issue Nov 25, 2018 · 14 comments
Labels

Comments

@alex996
Copy link

alex996 commented Nov 25, 2018

I'm running into an error in nodemon today, which only happens with esm using -r from the CLI. Here's a dead-simple reproduction:

// app.js
require('express')().listen(3000, () => console.log('Works'))
"scripts": {
  "dev": "nodemon -r esm app"
},
"dependencies": {
  "esm": "^3.0.84",
  "express": "^4.16.4"
},
"devDependencies": {
  "nodemon": "^1.18.6"
}

When I run yarn dev from the terminal, and then do Ctrl+S in app.js in the editor, I get

yarn run v1.12.3
$ nodemon -r esm app
[nodemon] 1.18.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node -r esm app index.js`
Works
[nodemon] restarting due to changes...
[nodemon] starting `node -r esm app index.js`
Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1294:14)
    at listenInCluster (net.js:1342:12)
    at Server.listen (net.js:1429:7)
    at Function.listen (/path/to/my/app/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/path/to/my/app/app.js:1)
    at Object.<anonymous> (/path/to/my/app/app.js:1)
Emitted 'error' event at:
    at emitErrorNT (net.js:1321:8)
    at internalTickCallback (internal/process/next_tick.js:72:19)
    at process._tickCallback (internal/process/next_tick.js:47:5)
    at /path/to/my/app/node_modules/esm/esm.js:1:31617
    at /path/to/my/app/node_modules/esm/esm.js:1:31302
    at process.<anonymous> (/path/to/my/app/node_modules/esm/esm.js:1:31588)
    at /path/to/my/app/node_modules/esm/esm.js:1:53673
    at Function.<anonymous> (/path/to/my/app/node_modules/esm/esm.js:1:251314)
    at Function.<anonymous> (/path/to/my/app/node_modules/esm/esm.js:1:251083)
    at Function.<anonymous> (/path/to/my/app/node_modules/esm/esm.js:1:241320)
[nodemon] app crashed - waiting for file changes before starting...

You can see that the app boots correctly, but as soon as any file changes, nodemon can't restart it. In the meantime, the app still continues to run in the background. If I do Ctrl+C, it quits, but there's no more process on port 3000, so killing it by port fuser -k 3000/tcp doesn't do anything.

I found that

  • 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 with esm using require("esm")(module) syntax instead of -r
  • it works either way without express or any web server (because the port is not used)
  • same issue on Node 11.2.0, 11.1.0, and 10.13.0

Tried rebooting, reinstalling Node, removing yarn.lock, then removing and re-installing node_modules, locking to older versions of esm, nodemon, and express... I'm out of options here.

At first, I thought this was nodemon's fault, so I scavenged many threads with this error, but to no avail. Then I noticed that the error appears to be emitted from esm, so I thought I'd post here. Sorry, if I'm posting in a wrong repo; it seems the culprit lies in this library. Any clues are appreciated!

  • OS: Ubuntu 18.10 cosmic
  • Node: 11.2.0 (other versions too, see above)
  • npm: 6.4.1
  • yarn: 1.12.3
@jdalton
Copy link
Member

jdalton commented Nov 25, 2018

Hi @alex996!

Thank you for digging into this!

I could repro the issue on macOS 10.14.1 on Node 11.2.0 with yarn dev but not with npm run dev. It does look like several folks have hit the EADDRINUSE issue with nodemon and yarn outside of esm though. I noticed that if I created an empty a.js file and made the dev script "nodemon -r ./a app" that I continued to reproduce the error with yarn and not npm. I think you should file an issue on yarn using this similar repro but with the dummy a.js file.

@jdalton jdalton closed this as completed Nov 25, 2018
@alex996
Copy link
Author

alex996 commented Nov 25, 2018

@jdalton Thanks so much for the info. I'll follow up with the issues in nodemon.

@Dannyzn
Copy link

Dannyzn commented Apr 8, 2019

windows 10 kill all about node.js in back

@mishra-vikash
Copy link

mishra-vikash commented Oct 18, 2019

I was using nodemon 1.17.5 by following a course, Now I have updated with the latest version 1.19.x, which has fixed my problem.

@krzywiecki
Copy link

I have no idea how this matter but my failing setup looked like this:

app.js file (entry point of the app)

const port = process.env.PORT || 8000;
....
....
....
server.listen(port, (error) => {
    if(error) {
        throw error;
    }

    console.log(`Express server listening on port ${port}`);
});

and my command to start the server was:

./node_modules/nodemon/bin/nodemon.js app.js

now I've changed the first line when I defined port constant from to this:

const port = process.env.PORT;

and now I'm defining the port right before starting nodemon like this:

PORT=8000 ./node_modules/nodemon/bin/nodemon.js app.js

So essentially before my process.env.PORT was not defined at all so it was taking the default 8000 defined whole assigning value to the port constant. Now it's just using the PORT defined straigh before running the server. I'd love to understand the logic behind it. Also, just running it with this port did not help. I had to remove || 8000 from the place when I declare port constant.

@sensunny
Copy link

In my case, Whenever I am saving the code, it is showing this port is already in use.
so in that case just restart the system. You will see the magic. Error is disappear

@little-boy
Copy link

Sometimes, it's just because nodemon is restarting faster than the process is being killed.

For example, if there is some (slow) logic in server.on('close') or server.on('SIGINT').

@zashishz
Copy link

zashishz commented Jul 3, 2020

I have solved this issue by adding below in my package.json for killing active PORT - 4000 (in my case)

"scripts": {
    "dev": "nodemon app.js",
    "predev":"fuser -k 4000/tcp && echo 'Terminated' || echo 'Nothing was running on the PORT'",
  }

Source

@SulaRanawake
Copy link

I also got this error when I ran the project with VScode terminals and closed multiple working terminals without killing them or without stopping the server. Killing every previous terminal and starting with a new terminal solved this for me.

@marcisp
Copy link

marcisp commented Oct 1, 2020

I had the same problem. Realized that I was using global nodemon version. Once I installed it locally, the problem was gone.

@abdelhak-ajbouni
Copy link

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

@egin10
Copy link

egin10 commented Jan 26, 2021

Thank's @AbdelhakAj, It works!

@nathanchilton
Copy link

I was using nodemon 1.17.5 by following a course, Now I have updated with the latest version 1.19.x, which has fixed my problem.

Thank you, @mishra-vikash! I was also using v1.17.5, as instructed by the course I am following. I upgraded to version 1.19 and the problem no longer occurring.

@guilhermecarolino35
Copy link

I have solved this issue by adding below in my package.json for killing active PORT - 4000 (in my case)

"scripts": {
    "dev": "nodemon app.js",
    "predev":"fuser -k 4000/tcp && echo 'Terminated' || echo 'Nothing was running on the PORT'",
  }

Source

That

I have solved this issue by adding below in my package.json for killing active PORT - 4000 (in my case)

"scripts": {
    "dev": "nodemon app.js",
    "predev":"fuser -k 4000/tcp && echo 'Terminated' || echo 'Nothing was running on the PORT'",
  }

Source

Thank you mate,that worked , I was having this issue in this version nodemon": "^2.0.12

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests