Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Bardala opened this issue Aug 14, 2023 · 6 comments
Closed
Labels
stale no activity for 2 weeks

Comments

@Bardala
Copy link

Bardala commented Aug 14, 2023

  • 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

@Bardala Bardala changed the title nodemon doesn't kill the last listening port when resaving the file nodemon doesn't kill the last listening port when resaving the project Aug 14, 2023
@mvaibhav77
Copy link

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.

@bradleyayers
Copy link

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.

@github-actions
Copy link

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

@github-actions github-actions bot added the stale no activity for 2 weeks label Sep 19, 2023
@basicdays
Copy link

basicdays commented Oct 5, 2023

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

@github-actions github-actions bot removed the stale no activity for 2 weeks label Oct 5, 2023
@github-actions
Copy link

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

@github-actions github-actions bot added the stale no activity for 2 weeks label Oct 19, 2023
Copy link

Automatically closing this issue due to lack of activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale no activity for 2 weeks
Projects
None yet
Development

No branches or pull requests

4 participants