RepoPilot

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

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

Question

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

RepoPilot

Yes, there is a helper function called portNumbers() to generate a sequence of port numbers in a specified range.

The portNumbers(from, to) function generates port numbers in the given range from from to to (inclusive). It returns an Iterable for port numbers, which can be passed to the port option of getPort().

Here's how it works:

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

The function is defined in [index.js:L169-L194] and validates that both from and to are:

  • Integer numbers
  • Within the range 1024 to 65535
  • With to being greater than or equal to from

It uses a generator function to yield each port number sequentially in the range [index.js:L184-L187].

FOLLOW_UPS:

  • How does getPort fall back if all preferred ports are unavailable?
  • What happens if I pass an invalid range to portNumbers?
  • Can I exclude specific ports from the available ports?

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.