0

I am working on a slack bot tutorial that runs express server and runs concurrently, nodemon, and ngrok.

The problem I am running into is:

Error: listen EADDRINUSE: address already in use :::5000

I think the error happens because nodemon re-runs express when an update happens. index.js:

const server = app.listen(process.env.PORT || 5000, () => {
  console.log('Express server listening on port %d in %s mode', server.address().port, app.settings.env);
});

I've gone down several rabbit holes that include parent and child 'processes' (https://dev.to/kyrelldixon/how-to-setup-an-express-js-server-in-node-js-56hp)

And / or it looks like this may be some kind of bug due to an update (https://github.com/remy/nodemon/issues/1025)

I have even thought about adding some kind of callback on .listen so app checks for whether or not a port is running (http://expressjs.com/en/5x/api.html#app.listen_path_callback)

But ultimately, not sure how to figure out the issue. Can anyone provide some guidance?

my setup:

npm -v
1.5.0
node -v
v10.16.3
nodemon -v
v2.0.2
express -v
v4.16.0
coachpacman
  • 71
  • 2
  • 9

2 Answers2

2

adding something like

process.on(‘SIGINT’, () => { console.log(“exiting…“); process.exit(); });

process.on(‘exit’, () => { console.log(“exiting…“); process.exit(); });

in your main js file should catch these situations and allow for you to make sure the process exits properly. You could expand to call a function to additional clean up as well

George D.
  • 321
  • 1
  • 4
  • 12
  • 1
    https://stackoverflow.com/questions/14031763/doing-a-cleanup-action-just-before-node-js-exits the accepted answer here might get you on track! good luck! – George D. Feb 25 '20 at 21:52
0

This happens when there is a service already running and acquiring that port.

Sometimes when you exit the application with Ctrl-c the process doesn't gets kill completely and keeps running in the background.

You can check which process is acquiring your port using this command

    netstat -tulnp | grep '<port number>'

enter image description here

Will look something like this ^^

And then kill that process with the process id provided by the above command.

57997 in my case

You can also check if there is any process running already in the background using ps command.

Saurabh Thakur
  • 139
  • 3
  • 4
  • Thanks for the reply - I can get out of this error when I kill processes individually, but it looks like I have to do this every time nodemon runs the app. Is there a way to handle this programmatically so I don't evfer need to check processes? – coachpacman Feb 25 '20 at 12:47
  • This [**nodemon** issue](https://github.com/remy/nodemon/issues/1109) may be the problem you are facing. One of the solution which solved the problem was updating to gitbash `2.16` as said [here](https://github.com/remy/nodemon/issues/1109#issuecomment-370981817) Can you try this? – Saurabh Thakur Feb 25 '20 at 14:57
  • Ah bummer `3.2.57(1)-release` – coachpacman Feb 25 '20 at 15:10