Pain points:

When starting a project, we often encounter a situation where a port is occupied, and it is too cumbersome to manually kill the process that occupies the current port number, so we expect to have a policy in place to adjust if the port is occupied.

Steps:

  1. Port sniffer

  2. If the port number is not occupied, start smoothly. If the port number is occupied, the port number +1 (if the port number is still occupied, continue the loop without exceeding the maximum port number), and ask the user whether to start the project with the adjusted port.

There are mature solutions to this pain point.

For step 1:

Port sniffing: detect-port-alt

For step 2:

Adjust port: react-dev-utils

Look at the source code implementation:

As you can see, the implementation is relatively simple. Port sniffing is also determined by detect-port-alt. Whether to start the project according to the adjusted port only needs to introduce the Inquirer.

Confucius said: isn’t it a pleasure to have a ready-made LIBRARY of NPM and use it directly?