1

I am using node Js, and I want to know if there is a way to make my nodeJs automatically change the PORT if the default one has been used.

for exemple: the default port for my server to run on is 3000. But if that port is already in use, it should maybe ask me if I want to switch port and then look up the first unused port to run on.

Thank you

1 Answers1

1

You can use this library to find a free port that is not being used incrementally. (There is still a slight chance for race condition a port might be used after the code is run, to mitigate that wrap it in a try catch block and call find-free-port again).

Tahlil
  • 1,031
  • 1
  • 14
  • 26