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/13/2026
Yes, there is. The portNumbers() helper function generates a sequence of port numbers within a specified range [index.js:L169-L194].
Here's how it works:
Function signature:
portNumbers(from, to)
Parameters:
from— The first port of the range. Must be an integer between 1024 and 65535.to— The last port of the range. Must be an integer between 1024 and 65535, and must be greater than or equal tofrom.
Returns: An iterable of port numbers in the given range.
Example usage:
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 is implemented as a generator [index.js:L186-L190] that yields each port sequentially from from to to. It validates that both parameters are integers and within the valid port range, throwing a TypeError or RangeError if validation fails [index.js:L170-L183].
FOLLOW_UPS:
- What ports are considered valid by portNumbers?
- Can you pass portNumbers output directly to getPort?
- How does getPort handle unavailable 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.