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/6/2026
Yes, you can bind to a specific host using the host option. Here's how it works:
The host Option
The host option is part of the Options type [index.d.ts:L3-L65] and allows you to specify either an IPv4 or IPv6 address for port resolution. When provided, getPort() will only check availability on that specific host rather than all local addresses.
How It's Passed Through
The host option is passed through the call chain:
-
Into
getPort(): You pass it as part of the options object:await getPort({port: 3000, host: '127.0.0.1'}) -
Through to
getAvailablePort(): In [index.js:L61-L78], when you provide ahostoption, it gets passed directly tocheckAvailablePort():if (options.host || options.port === 0) { return checkAvailablePort(options); } -
To the server listener: In [index.js:L47-L60], the options (including
host) are passed toserver.listen():server.listen(options, () => { const {port} = server.address(); // ... });
The host option is spread into netOptions [index.js:L122] and becomes part of the options passed to checkAvailablePort(), which ultimately passes it to Node's native server.listen() method.
When host is omitted, the library checks availability across all local addresses instead.
FOLLOW_UPS:
- What happens when host is omitted?
- How does ipv6Only option interact with host?
- Can I use IPv6 addresses with the host option?
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.