Language : 🇺🇸 English | 🇨🇳 简体中文
port-selector
is a cross-platform NodeJS library implemented in Rust. It mainly provides port availability checking and filtering ports based on filter conditions.
- Node.js (>= 10.0.0 required, LTS preferred)
npm install port-selector
# or
yarn add port-selector
# or
pnpm i port-selector
import {
Selector,
isFree,
isFreeTcp,
isFreeUdp,
randomFreePort,
randomFreeTcpPort,
randomFreeUdpPort,
selectFreePort,
selectFromGivenPort
} from 'port-selector'
isFree · isFreeTcp · isFreeUdp · randomFreePort · randomFreeTcpPort · randomFreeUdpPort · selectFromGivenPort · selectFreePort
Check whether the port is not used on TCP and UDP
function isFree(port: number): boolean
Check whether the port is not used on TCP
function isFreeTcp(port: number): boolean
Check whether the port is not used on UDP
function isFreeUdp(port: number): boolean
The system randomly assigns available TCP and UDP ports
function randomFreePort(): number
The system randomly assigns available TCP ports
function randomFreeTcpPort(): number
The system randomly assigns available UDP ports
function randomFreeUdpPort(): number
Check from starterPort
and return the first available port
Return if starterPort
is available; Otherwise starterPort += 1
until the port is available
function selectFromGivenPort(starterPort: number): number
Gets a matching port based on the Selector
parameter constraint
function selectFreePort(selector?: Selector): number
export type Selector = {
// Check whether the port is available on TCP.
// The default value is true.
checkTcp?: boolean
// Check whether the port is available on UDP.
// The default value is true.
checkUdp?: boolean
// Set the generated port range, starting value
// The default value is 0.
portFrom?: number
// Set the generated port range, end value
// The default value is 65535.
portTo?: number
// Maximum number of random times. Default value: 100
// If no available port number is found within the maximum random number of loops, None is returned
maxRandomTimes?: number
}