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 5/18/2026
Question
RepoPilot
Yes, there is a portNumbers() helper function. It generates port numbers in a specified range that can be passed to getPort() as preferred ports to try [index.js:L169-L194].
The function signature takes two parameters:
- from: The first port of the range (must be between 1024 and 65535)
- to: The last port of the range (must be between 1024 and 65535 and greater than
from)
It returns an Iterable of port numbers in that range [index.d.ts:L66-L82].
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
Internally, the portNumbers() function uses a generator function to yield ports sequentially from the from value up to and including the to value [index.js:L182-L186].
FOLLOW_UPS:
- What validation does portNumbers perform on inputs?
- How does getPort handle the iterable from portNumbers?
- Can you exclude ports from the generated range?
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.