RepoPilot

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

About sindresorhus/get-port · 2 messages · last activity 7/27/2026

Question

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

RepoPilot

Yes, there is a helper function called portNumbers() that generates a sequence of port numbers in a given range.

The portNumbers(from, to) function [index.js:L169-L194] generates port numbers within a specified range and returns an Iterable that can be passed to the port option of getPort().

Here's how it works:

  • Parameters: It takes two integer arguments, from and to, both of which must be between 1024 and 65535
  • Returns: An iterable (generator) that yields all port numbers from from to to (inclusive)
  • Validation: The function validates that both parameters are integers, within the valid port range, and that to is greater than or equal to from [index.js:L169-L184]

Example usage [index.d.ts:L76-L80]:

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 try a specific range of ports in order, falling back to a random available port if none in the range are available.

FOLLOW_UPS:

  • What validation errors does portNumbers throw?
  • How does getPort handle the exclude option?
  • Can you use portNumbers with preferred 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.