RepoPilot

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

About sindresorhus/get-port · 2 messages · last activity 7/6/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 in a given range.

[index.js:L169-L194] shows the implementation. It's a generator function that yields all integers from from to to (inclusive). You can use it like this:

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 that:

  • Both from and to are integers
  • Both are in the range 1024...65535
  • to is greater than or equal to from

This is useful when you want getPort() to check a specific range of ports before falling back to a random available port.

FOLLOW_UPS:

  • What happens when all ports in the range are taken?
  • Can you exclude specific ports from consideration?
  • How does the reserve option affect port availability?

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.