getAvailablePort(options?: GetAvailablePortOptions): number
Returns an available network port.
Important
In most cases, this function is not needed. Do not use it for trivial uses
such as when using Deno.serve
or Deno.listen
directly. Instead, set the port
option to 0
to automatically use an
available port, then get the assigned port from the function's return
object (see "Recommended Usage" example).
Recommended Usage
Recommended Usage
Bad:
import { getAvailablePort } from "@std/net/get-available-port"; const port = getAvailablePort(); Deno.serve({ port }, () => new Response("Hello, world!"));
Good:
const { port } = Deno.serve({ port: 0 }, () => new Response("Hello, world!")).addr;
Good:
import { getAvailablePort } from "@std/net/get-available-port"; const command = new Deno.Command(Deno.execPath(), { args: ["test.ts", "--port", getAvailablePort().toString()], }); // ...
optional
options: GetAvailablePortOptions
Options for getting an available port.
An available network port.