Skip to main content
Home

@std/net@1.0.4
Built and signed on GitHub Actions

Utilities for working with the network

This package works with DenoIt is unknown whether this package works with Bun
This package works with Deno
It is unknown whether this package works with Bun
JSR Score
94%
Published
10 months ago (1.0.4)
function getAvailablePort
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).

Examples

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()],
});
// ...

Parameters

optional
options: GetAvailablePortOptions

Options for getting an available port.

Return Type

An available network port.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@std/net

Import symbol

import { getAvailablePort } from "@std/net/get-available-port";
or

Import directly with a jsr specifier

import { getAvailablePort } from "jsr:@std/net/get-available-port";

Add Package

bunx jsr add @std/net

Import symbol

import { getAvailablePort } from "@std/net/get-available-port";