Question
Is there a helper to generate a sequence of port numbers to try?
About sindresorhus/get-port · 2 messages · last activity 7/6/2026
Is there a helper to generate a sequence of port numbers to try?
Yes, there is. The portNumbers() helper function generates a sequence of port numbers in a given range.
[index.js:L169-L194] shows the implementation. It's a generator function that yields all integers from from to to (inclusive). 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 validates that:
from and to are integers1024...65535to is greater than or equal to fromThis is useful when you want getPort() to check a specific range of ports before falling back to a random available port.
FOLLOW_UPS:
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.