Is there a helper to generate a sequence of port numbers to try?
Is there a helper to generate a sequence of port numbers to try?
About sindresorhus/get-port · 2 messages · last activity 7/20/2026
Yes, there is. The portNumbers() helper function generates a sequence of port numbers in a given range.
According to [index.js:L169-L194], portNumbers(from, to) returns a generator that yields all port numbers from from to to (inclusive). It validates that both parameters are integers within the range 1024...65535 and that to is greater than or equal to from.
You can use it like this [readme.md:L34-L37]:
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 accepts the port option which can be number | Iterable<number> [index.d.ts:L5-L7], so it's designed to work seamlessly with iterables like the generator returned by portNumbers().
FOLLOW_UPS:
- How does getPort fall back if preferred ports are unavailable?
- What happens when portNumbers receives invalid parameters?
- Can you exclude specific ports from consideration?
Want to ask your own question?
Open chat for sindresorhus/get-portPublic 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.