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 5/25/2026
Yes, you can bind to a specific host using the host option in the options object passed to getPort().
According to the API documentation in [readme.md:L80-L87], the host option is:
Type:
stringThe 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.
The host option is passed through to the underlying port checking mechanism. In [index.js:L61-L78], the getAvailablePort function handles this:
const getAvailablePort = async (options, hosts) => {
if (options.host || options.port === 0) {
return checkAvailablePort(options);
}
// ... checks all hosts if no host specified
};
When you specify a host, it directly calls checkAvailablePort(options) which then passes the options to server.listen() [index.js:L47-L60]. The options object (including the host property) is passed directly to Node's net.createServer().listen() method, which uses the host to bind to that specific address.
For example, you can call:
await getPort({host: '127.0.0.1', port: 3000});
This will check if port 3000 is available specifically on the 127.0.0.1 address.
FOLLOW_UPS:
- What happens when host option is not specified?
- How does the exclude option work with host?
- Can I use IPv6 addresses with the host option?
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.