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 6/1/2026
Yes, you can bind to a specific host using the host option.
According to the API documentation [readme.md:L80-L88], the host option is of type string and can be either an IPv4 or IPv6 address:
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.
How it's passed through:
The host option is part of the Options type [index.d.ts:L3-L65], which includes Omit<ListenOptions, 'port'> — meaning it accepts all standard Node.js ListenOptions properties except port. This allows the host parameter to be passed directly to Node.js's server.listen() function.
In the implementation [index.js:L47-L60], the options are passed to server.listen(options, ...), so the host value gets forwarded directly to Node.js's underlying networking layer.
Example usage from tests:
The test suite demonstrates this in action [test.js:L59-L69]:
test('preferred port given IPv4 host', async t => {
const desiredPort = 8081;
const port = await getPort({
port: desiredPort,
host: '0.0.0.0',
});
t.is(port, desiredPort);
});
FOLLOW_UPS:
- What happens if no host option is specified?
- Can reserved ports be blocked across different hosts?
- How does ipv6Only option interact with host selection?
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.