Error: listen EADDRINUSE when using nodemon with babel 7

2k views Asked by At
  • nodemon -v: 1.18.6
  • node -v: v11.2.0
  • Command: nodemon --exec babel-node lib/server.js

My app starts ok with the above command. When I change the source code, I expect the nodemon restart automatically. But I got the error Error: listen EADDRINUSE: address already in use :::3000.

Error details

[nodemon] starting `babel-node lib/server.js`
events.js:167
      throw er; // Unhandled 'error' event
      ^

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 (/Users/longntran/Desktop/learning/pangara-web/node_modules/express/lib/application.js:618:24)
    at Object.listen (/Users/longntran/Desktop/learning/pangara-web/lib/server.js:12:5)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Module._compile (/Users/longntran/Desktop/learning/pangara-web/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Object.newLoader [as .js] (/Users/longntran/Desktop/learning/pangara-web/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:620:32)
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 Function.Module.runMain (internal/modules/cjs/loader.js:778:11)
    at Object.<anonymous> (/Users/longntran/Desktop/learning/pangara-web/node_modules/@babel/node/lib/_babel-node.js:224:23)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3) 

Dependencies

"dependencies": {
    "ejs": "^2.6.1",
    "express": "^4.16.4",
    "react": "^16.6.3",
    "react-dom": "^16.6.3"
  },
  "devDependencies": {
    "@babel/core": "^7.1.6",
    "@babel/preset-env": "^7.1.6",
    "@babel/preset-react": "^7.0.0",
    "@babel/node": "^7.0.0",
    "babel-jest": "^23.6.0",
    "babel-loader": "^8.0.4",
    "jest": "^23.6.0",
    "nodemon": "^1.18.6",
    "pm2": "^3.2.2",
    "prettier": "1.15.2",
    "regenerator-runtime": "^0.13.1",
    "webpack": "^4.26.1",
    "webpack-cli": "^3.1.2"
  }

Note

I am using nodemon with babel 7. Please help me. Thank you guys

2

There are 2 answers

0
LennonR On

I found an interesting fix in this thread around adding a delay and kill process commands to your scripts.

"events": {
  "restart": "fuser -k 5000/tcp ; fuser -k 3050/tcp" // change to whatever ports you're using.
}

And to add a delay to the nodemon command:

 --delay 1500ms

https://github.com/remy/nodemon/issues/1316#issuecomment-387367160

I also noticed that if I hit save twice in my code editor in rapid succession, I get this error. However if I only hit save once, I see no problem.

0
SenAnan On

I've found that completing a graceful server shutdown on any interruption allows nodemon to restart successfully (using Express):

// Run server
const PORT = process.env.PORT || 3000
const server = app.listen(PORT, () => {
    console.log('[server] Started server');
});

// Graceful shutdown of server
process.on('SIGINT', () => {
    console.log('\n[server] Shutting down...');
    server.close();
    process.exit();
});

process.on('SIGTERM', () => {
    console.log('\n[server] Shutting down...');
    server.close();
    process.exit();
});

process.on('uncaughtException', () => {
    console.log('\n[server] Shutting down...');
    server.close();
    process.exit();
});