RepoPilot

Is there a helper to generate a sequence of port numbers to try?

About sindresorhus/get-port · 2 messages · last activity 6/8/2026

Question

Is there a helper to generate a sequence of port numbers to try?

RepoPilot

Yes, there is. The portNumbers() helper function generates a sequence of port numbers within a specified range.

According to the README, portNumbers(from, to) returns an Iterable for port numbers in the given range. You can pass the result directly to the port option of getPort():

import getPort, {portNumbers} from 'get-port';

console.log(await getPort({port: portNumbers(3000, 3100)}));
// Will use any port from 3000 to 3100, otherwise fall back to a random port

This is useful when you want to check a specific range of ports rather than just a single preferred port or arbitrary list.

FOLLOW_UPS:

  • How does portNumbers() generate the iterable internally?
  • Can you combine portNumbers with the exclude option?
  • What happens if all ports in range are unavailable?

Want to ask your own question?

Open chat for sindresorhus/get-port

Public Q&A. Generated by RepoPilot from the actual source of sindresorhus/get-port. AI answers can be incomplete or stale — verify before relying on them.