nodemon: Error: listen EADDRINUSE

  • nodemon -v: 1.17.3
  • node -v: 8.10.0
  • Operating system/terminal environment: MacOS 10.13.4
  • Command you ran: nodemon app.js

Expected behaviour

Should restart the app when there are changes using the same http server (host & server)

Actual behaviour

Restarts the app but throwing an error saying the EADDRINUSE.

Steps to reproduce

  1. Create a small ExpressJS app. At least 2 files: app.js and dummy.js
  2. Let app.js require dummy.js
  3. Listen to a host (i.e. 127.0.0.1) and port (i.e. 4000)
  4. run nodemon app.js
  5. Edit and save dummy.js

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 44 (10 by maintainers)

Most upvoted comments

Solution for Googlers…

For me the problem happens due to use of concurrently (which itself is a workaround the problem that nodemon doesn’t understand I need more then one process running). If I’m not mistaken concurrently closes and nodemon considers job done.

Even with out use of concurrently I’ve had plenty of similar issues in the past due to conflicts of nodemon exiting and the process exiting.

You can solve practically all nonsensical port related problems like this:

"events": {
	"restart": "fuser -k 5000/tcp ; fuser -k 3050/tcp"
}

Essentially the above says “kill anything running on TCP ports 5000 and 3050”

You may also wish to add this in a script before nodemon starts:

# replace with your own ports
fuser -k 5000/tcp 
fuser -k 3050/tcp

Also you NEED to add this too to the nodemon command:

 --delay 1500ms

If there is no delay then 2 times out of 10 you’ll get some port conflict.

The delay also prevents nodemon from spamming restarts since it resets every new change (preventing yet another thing that causes conflicts).

It would be nicer if killing ports before restart was just something that was built in (along with the ability to execute multiple scripts at once).

cc @remy

I was having the same problem and in the latest version - 1.18.7 this seems to be fixed. Cheers!!

A Node.js style solution (work in Mac, Linux, Windows):

yarn add kill-port

in nodemon.json:

{
  "events": {
    "restart": "kill-port 5000",
    "crash": "kill-port 5000"
  },
 "delay": "1500"
}

Replace your port:

"restart": "kill-port [my port]",

1.18.7 has same problem on here.

@LBWright thanks! Was having the same issue but killall node fixed it.

Yes! 1.18.7 is working!

My solution:

nodemon.json

{
  "execMap": {
    "js": "babel-node"
  },
  "env": {
    "NODE_ENV": "development"
  },
  "ext": ".js,.jsx",
  "ignore": [
    "test",
    "dist",
    "docs",
    "logs"
  ],
  "events": {
    "start": "sh nodemon.sh"
  },
  "script": "./src/server.js",
  "restartable": "rs",
  "verbose": false,
  "watch": ["src"],
  "stdin": false,
  "stdout": true,
  "quiet": true,
  "colours": true
}

nodemon.sh

PID=$(ps aux | grep _babel-node | sed -n '2p' | awk '{print $2}');

if [ ! -z "$PID" ]; then
  { kill -9 $PID && wait $PID; } 2>/dev/null;
fi;

src/server.js

import { exec } from 'child_process';

/* ...skipped for brevity... */

process.on('uncaughtException', (err) => {
  if (/EADDRINUSE/.test(err.message)) {
    exec(`sh nodemon.sh`);
  } else {
    console.error("uncaughtException:", err.message);
  }
});

// or better:
server.on('error', err => {
  if (err.code === 'EADDRINUSE') {
    exec(`sh nodemon.sh`);
  }
});

The trick is to kill the port whenever nodemon start, not when it restarts/crash.

@remy Nodemon process sustains after parent process being killed (Webpack out of memory or something similar) in my case. Here are codes to replicate this case.

@remy Can replicate via nestjs sample project:

nest docs: https://docs.nestjs.com/first-steps

$ npm i -g @nestjs/cli
$ nest new project

Update any .ts file in sample project, you will see Error: listen EADDRINUSE

My env: MacOS,Node v10.9.0, “nodemon”: “^1.18.6”,

I had the same issue that I fixed by adding --signal SIGTERM

I haven’t been able to replicate it since I had the error. When I did encounter the error again (only one more time), I used the killall node command and I haven’t had the error since. It might be an that the OS isn’t killing the process before the server restarts. But I can only speculate. I’m sorry that I haven’t been much help, I’m still relatively new to the dev world and I’m trying to provide as much help as I can.

Edit: if I encounter the error, I will immediately retest with nodemon --signal SIGTERM.

This has been mentioned again recently on a closed issue. https://github.com/remy/nodemon/issues/1025#issuecomment-381025138.

I’ll try to create another test and hopefully I could pinpoint the cause.