remy / nodemon

Monitor for any changes in your node.js application and automatically restart the server - perfect for development

Home Page:http://nodemon.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nodemon doesn't kill the last listening port when resaving the project

Bardala opened this issue · comments

  • Versions: node@v19.2.0, win32@10.0.19045
  • nodemon -v: 3.0.1
  • Operating system/terminal environment (powershell, gitshell, etc): powershell
  • Using Docker? What image: No
  • Command you ran: npm start

nest@1.0.0 start
npm-run-all --parallel start:backend start:frontend

nest@1.0.0 start:backend
cd backend && npm start

nest@1.0.0 start:frontend
cd frontend && npm start

backend@1.0.0 start
nodemon src/server.ts

frontend@0.1.0 start
craco start

Expected behaviour

  • The Expected behaviour is running the project on the same port when I resave it

[nodemon] restarting due to changes...
[nodemon] starting ts-node src/server.ts
Server listening on port 4001

Actual behaviour

  • the port still in use from the last calling, so I kill it manually

[nodemon] starting ts-node src/server.ts
Error: listen EADDRINUSE: address already in use :::4001
at Server.setupListenHandle [as _listen2] (node:net:1468:16)
at listenInCluster (node:net:1516:12)
at Server.listen (node:net:1604:7)
at {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 4001
}
[nodemon] app crashed - waiting for file changes before starting...

stackoverflow

Hi @Bardala,

Here are some methods you can try to solve your problem:-

  • Check and Free Port: Verify that port 4001 is not being utilized by another process. If there's a conflict, try using a different port to avoid the issue.

  • Separate Frontend and Backend Ports: Make sure the frontend and backend are running on separate ports to prevent conflicts. Adjust the configurations to assign distinct ports for each part of the application.

  • Avoid Duplicate listen() Calls: Ensure that your backend code doesn't contain two instances of server.listen() or any equivalent function that sets up a server. Having multiple listen calls can result in address conflicts.

  • Terminate Previous Processes: If the error occurs during the initial run, you can try closing the process running on the desired port (in this case, port 4001) using the command npx kill-port port-number.

If the issue persists, feel free to reach out for further assistance. I'm here to help.

This happens to me too if I try to run two commands in sequence i.e. if I prefix exec with true && then I run into the problem where the server isn't shut down properly and I get EADDRINUSE. As soon as I remove the true && it's reliably frees the port when restarting.

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

The only reliable way I've found to handle graceful restarts with nodemon was to catch SIGUSR2 process signal and perform a httpServer.close() in the callback. This should hopefully allow your application code to free up its resources before nodemon aggressively restarts it again. See docs here.

If your graceful exit code requires anything async though, you'll get random race conditions since nodemon isn't properly waiting for the process to exit before starting it again. See issue #1889

This issue has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automtically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and @remy will (try!) to follow up.
Thank you for contributing <3

Automatically closing this issue due to lack of activity