Skip to content

Instantly share code, notes, and snippets.

@normancarcamo
Last active November 24, 2018 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save normancarcamo/7f5d5655525ff832e01f68953f966417 to your computer and use it in GitHub Desktop.
Save normancarcamo/7f5d5655525ff832e01f68953f966417 to your computer and use it in GitHub Desktop.
Fix issues with: "Error: listen EADDRINUSE" and nodemon

nodemon.json

{
  "execMap": {
    "js": "babel-node"
  },
  "env": {
    "NODE_ENV": "development"
  },
  "ext": ".js,.jsx",
  "ignore": [
    "test",
    "dist",
    "docs",
    "logs"
  ],
  "events": {
    "restart": "sh ./nodemon.sh",
    "crash": "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 $PID && wait $PID; } 2>/dev/null;
fi;

src/server.js

import { exec } from 'child_process';

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

process.on('uncaughtException', (err) => {
  if (err.message.includes("EADDRINUSE")) {
    exec(`sh ./nodemon.sh`);
  } else {
    console.error("uncaughtException:", err.message);
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment