Skip to main content
Deno 2 is finally here 🎉️
Learn more

deno-port

Deno module to check if a port is available, get an available TCP port, or kill a process running on a port.

Import Module

import {
  getAvailablePort,
  getAvailablePortSync,
  isPortAvailable,
  isPortAvailableSync,
  killProcessOnPort
} from "https://deno.land/x/port/mod.ts"

Usage

isPortAvailable(options)

Requires --allow-net flag
Returns a promise which resolves to true if a given port is available, else false.

isPortAvailableSync(options)

Requires --allow-net flag
Returns true if a given port is available, else false.

options

Type: object

port

Required
Type: number

hostname

Type: string

transport

Type: string
Values: tcp


getAvailablePort(options)

Requires --allow-net flag
Returns a promise which resolves to an available port number based on the options provided.

getAvailablePortSync(options)

Requires --allow-net flag
Returns an available port number based on the options provided.

options

Type: object

port

Type: number[] | { start: number, end: number }
If not provided, a random port will be returned
If an array of ports is provided, the first encountered available port in the array will be returned
If a range is provided, the first encountered available port in the range will be returned

hostname

Type: string

transport

Type: string
Values: tcp


killProcessOnPort(port)

Requires --allow-run and --allow-net flag
Kills the process running on the given port. Returns true if the process is killed successfully and the port is free, else false.

port

Required
Type: number

Test

deno test –allow-net mode_test.ts

Licensing

MIT licensed