RepoPilot

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

About sindresorhus/get-port · 2 messages · last activity 9/1/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.

The function is exported from [index.js:L169-L194] and accepts two parameters:

  • from: The first port of the range (must be between 1024 and 65535)
  • to: The last port of the range (must be between 1024 and 65535 and greater than or equal to from)

It returns an iterable (a generator) that yields all port numbers in the range from from to to (inclusive).

Example usage [readme.md:L98-L101]:

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 validates its inputs and throws errors if parameters are not integers [index.js:L170-L171], out of the valid port range [index.js:L173-L179], or if from is greater than to [index.js:L181-L183].

FOLLOW_UPS:

  • How does getPort handle multiple preferred ports?
  • What happens when all preferred ports are unavailable?
  • Can I exclude specific ports from being returned?

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.