nodemon: express server process dosn't get killed on ubuntu 16.04 so nodemon fail to restart the app

  • nodemon: ^2.0.4
  • node -v: v14.4.0
  • Operating system/terminal environment: Ubuntu 16.04.6 LTS
  • Using Docker? What image: no
  • Command you ran: npm run dev

Expected behaviour

to close current open server process and lunch a new one when the file changes

Actual behaviour

fail to restart the server because the process is already running on the same port



root@medo:~/ECommerceApp# npm run dev

> ecomerce-app@1.0.0 dev /root/ECommerceApp
> nodemon index.js

[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
App listening on port 3000
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1313:16)
    at listenInCluster (net.js:1361:12)
    at Server.listen (net.js:1447:7)
    at Function.listen (/root/ECommerceApp/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/root/ECommerceApp/index.js:40:5)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1340:8)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  code: 'EADDRINUSE',
  errno: -98,
  syscall: 'listen',
  address: '::',
  port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
^C         
root@medo:~/ECommerceApp# lsof -i:3000
COMMAND  PID USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
node    4887 root   19u  IPv6 4033651895      0t0  TCP *:3000 (LISTEN)
root@medo:~/ECommerceApp# 

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 21 (3 by maintainers)

Most upvoted comments

Note that nodemon is not responsible of closing the express connection, your server is.

Nodemon cannot actually know which socket the http server bound to on the operating system level and thus cannot cut the connection, so if you do not gracefully shut down your server then there is a good chance that the socket remains bound until it times out which can happen after the process has already exited and nodemon restarts it.

I wrote a bit more detailed solution in this other issue: https://github.com/remy/nodemon/issues/1748#issuecomment-680420175

If anyone wants to understand what is happening here, imagine and old-school landline phone call: you call your friend and you start talking and at some point your call accidentally disconnects and you immediately dial back (e.g. nodemon restarts the server). But because your friend didn’t notice right away that you disconnected and they don’t close their line right away, their landline is still active and all you get is a busy signal on your end. Just because one side closed the connection, doesn’t mean both sides did that at the same time. The call is successfully/gracefully terminated only when you both agree to hang up and finish the call.

So don’t be a jerk and just hang up, tell your friend (http server) to close the connection too! 😉