Can I bind to a specific host? How is the host option passed through?
Can I bind to a specific host? How is the host option passed through?
About sindresorhus/get-port · 2 messages · last activity 7/27/2026
Yes, you can bind (or rather, check availability) against a specific host via the host option.
API surface
The host option is defined as part of Options in the type definitions:
"The host on which port resolution should be performed. Can be either an IPv4 or IPv6 address. By default, it checks availability on all local addresses defined in OS network interfaces. If this option is set, it will only check the given host." [index.d.ts:L36-L40]
How it flows through the code
- In the main exported function, all options except
reserveare spread intonetOptions, which includeshostif you passed it:
const {reserve, ...netOptions} = options ?? {};
[index.js:L119]
-
For each candidate port in the check sequence,
getAvailablePortis called with{...netOptions, port}— sohostrides along as part of the options object passed down [index.js:L138]. -
Inside
getAvailablePort, if ahostwas explicitly provided (orport === 0), it skips iterating over all local network interfaces and just checks that single host directly:
const getAvailablePort = async (options, hosts) => {
if (options.host || options.port === 0) {
return checkAvailablePort(options);
}
...
[index.js:L61-L64]
If no host is given, it instead loops over all local hosts (from OS network interfaces) to verify the port is free on every interface before returning it [index.js:L61-L76].
checkAvailablePortultimately does the real work — it creates anet.Server, callsserver.listen(options, ...)(whereoptionsincludes yourhost/port), and resolves with the bound port once available [index.js:L47-L60]. Node'snet.Server.listenaccepts ahostin its options, so this is exactly where your host takes effect.
Test coverage confirming this behavior
preferred port given IPv4 hostbinds to0.0.0.0explicitly and confirms the desired port is returned [test.js:L54-L62].port can be bound to IPv4 host when promise resolvesgets a port withhost: '0.0.0.0'and verifies a server can bind to it on that host [test.js:L44-L52].preferred ports is bound up with different hostsshows how binding a preferred port on one host (e.g.,127.0.0.1) doesn't block it being returned for a different host, since availability is host-scoped [test.js:L161-L177].- There's also interaction with
reserve: reserving a port blocks it globally by port number regardless of host, even though normal (non-reserved) locking is host-specific — seereserve option blocks the same port on other hosts too[test.js:L206-L212] and the doc note: "Reserved ports are locked globally by port number for the current process, even if you looked them up with a specifichostoripv6Onlyoption." [index.d.ts:L18].
So in short: host is just forwarded straight through the options chain into net.Server.listen(), and its presence changes whether get-port checks a single host or all local interfaces.
FOLLOW_UPS:
- How does the
ipv6Onlyoption interact withhost? - What does
clearLockedPorts()actually reset? - How does
portCheckSequencedecide port order?
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.