Get an available TCP port to listen
Install package:
npm i get-port-please
// ESM
import {
getPort,
checkPort,
getRandomPort,
waitForPort,
getSocketAddress,
isSocketSupported,
cleanSocket
} from "get-port-please";
// CommonJS
const {
getPort,
checkPort,
getRandomPort,
waitForPort,
getSocketAddress,
isSocketSupported,
cleanSocket
} = require("get-port-please");
getPort(options: GetPortOptions): Promise<number>;
checkPort(port: number, host?: string): Promise<number | false>
waitForPort(port: number, options): Promise<number | false>
Try sequence is: port > ports > random
interface GetPortOptions {
name?: string;
random?: boolean;
port?: number;
portRange?: [fromInclusive: number, toInclusive: number];
ports?: number[];
host?: string;
memoDir?: string;
memoName?: string;
}
nameUnique name for port memorizing. Default is default.
randomIf enabled, port and ports will be ignored. Default is false.
portFirst port to check. Default is process.env.PORT || 3000
portsExtended ports to check.
portRangeExtended port range to check.
The range's start and end are inclusive, i.e. it is [start, end] in the mathematical notion.
Reversed port ranges are not supported. If start > end, then an empty range will be returned.
alternativePortRangeAlternative port range to check as fallback when none of the ports are available.
The range's start and end are inclusive, i.e. it is [start, end] in the mathematical notion.
Reversed port ranges are not supported. If start > end, then an empty range will be returned.
The default range is [3000, 3100] (only when port is unspecified).
hostThe host to check. Default is process.env.HOST otherwise all available hosts will be checked.
MIT
Overall
Maintained
Code review
Latest release
Releases this year
2,631 followers · starred Sep 2023
647 followers · starred Nov 2023
105 followers · starred May 2025
182 followers · starred Apr 2024
Get an available TCP port to listen
Install package:
npm i get-port-please
// ESM
import {
getPort,
checkPort,
getRandomPort,
waitForPort,
getSocketAddress,
isSocketSupported,
cleanSocket
} from "get-port-please";
// CommonJS
const {
getPort,
checkPort,
getRandomPort,
waitForPort,
getSocketAddress,
isSocketSupported,
cleanSocket
} = require("get-port-please");
getPort(options: GetPortOptions): Promise<number>;
checkPort(port: number, host?: string): Promise<number | false>
waitForPort(port: number, options): Promise<number | false>
Try sequence is: port > ports > random
interface GetPortOptions {
name?: string;
random?: boolean;
port?: number;
portRange?: [fromInclusive: number, toInclusive: number];
ports?: number[];
host?: string;
memoDir?: string;
memoName?: string;
}
nameUnique name for port memorizing. Default is default.
randomIf enabled, port and ports will be ignored. Default is false.
portFirst port to check. Default is process.env.PORT || 3000
portsExtended ports to check.
portRangeExtended port range to check.
The range's start and end are inclusive, i.e. it is [start, end] in the mathematical notion.
Reversed port ranges are not supported. If start > end, then an empty range will be returned.
alternativePortRangeAlternative port range to check as fallback when none of the ports are available.
The range's start and end are inclusive, i.e. it is [start, end] in the mathematical notion.
Reversed port ranges are not supported. If start > end, then an empty range will be returned.
The default range is [3000, 3100] (only when port is unspecified).
hostThe host to check. Default is process.env.HOST otherwise all available hosts will be checked.
MIT
Overall
Maintained
Code review
Latest release
Releases this year
2,631 followers · starred Sep 2023
647 followers · starred Nov 2023
105 followers · starred May 2025
182 followers · starred Apr 2024