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/15/2026
Yes, there is a helper function called portNumbers() that generates a sequence of port numbers within a specified range.
The portNumbers(from, to) function [index.js:L169-L194] generates port numbers in the given range from from to to (inclusive). It returns an Iterable that can be passed to the port option in getPort().
Here's how to use it:
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
Constraints:
- Both
fromandtomust be integers in the range1024...65535[index.js:L173-L180] tomust be greater than or equal tofrom[index.js:L182-L184]
The function validates its inputs and throws a TypeError if the arguments aren't integers, or a RangeError if they're outside the valid range or to is less than from [index.js:L170-L184].
FOLLOW_UPS:
- How does the exclude option work with portNumbers?
- What happens when all preferred ports are unavailable?
- Can I use portNumbers with other iterables?
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.