RepoPilot

Can I bind to a specific host? How is the host option passed through?

About sindresorhus/get-port · 2 messages · last activity 5/18/2026

Question

Can I bind to a specific host? How is the host option passed through?

RepoPilot

Yes, you can bind to a specific host using the host option in the getPort() function.

How to use it:

According to [index.d.ts:L57-L62], the host option is defined as:

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.

Examples from tests:

The test suite shows practical usage. For instance, [test.js:L48-L55] demonstrates passing an IPv4 host:

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);
});

How it's passed through:

Looking at the Options type definition in [index.d.ts:L3-L65], the host option is part of the Options object that gets passed to getPort(). Additionally, the Options type uses & Omit<ListenOptions, 'port'> at line 64, which means the host option is ultimately passed through to Node.js's underlying net.Server.listen() method (since ListenOptions comes from that API).

FOLLOW_UPS:

  • What happens when host is not specified?
  • Can I use IPv6 addresses with the host option?
  • How does reserve option interact with host?

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.