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 6/29/2026
Yes, there is. The portNumbers() helper function generates a sequence of port numbers in a given range [index.d.ts:L83].
According to the documentation, portNumbers(from, to) returns an Iterable for port numbers in the given range. The from parameter must be in the range 1024...65535, and to must also be in that range and greater than from [index.d.ts:L66-L82].
Here's how you use it [readme.md:L37-L40]:
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 check a specific range of ports before falling back to a random available port.
FOLLOW_UPS:
- What happens if no preferred ports are available?
- How does the reserve option affect port locking behavior?
- What's the difference between exclude and preferred ports?
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.