Question
Is there a helper to generate a sequence of port numbers to try?
About sindresorhus/get-port · 2 messages · last activity 5/25/2026
Is there a helper to generate a sequence of port numbers to try?
Yes, there is. The portNumbers() helper generates a sequence of port numbers in a given range [index.js:L169-L194].
You can use it like this:
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 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)The portNumbers() function returns an iterable that generates port numbers sequentially from from to to [index.js:L183-L188], which can then be passed to the port option of getPort() [index.d.ts:L3-L9].
FOLLOW_UPS:
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.