Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
get-port-please
Advanced tools
Get an available TCP port to listen
Weekly downloads
Package description
The `get-port-please` npm package is designed to help developers find an available port on the host machine. It is particularly useful in scenarios where an application needs to start a server but must avoid port conflicts with other applications. This package offers flexibility in selecting ports, including the ability to specify preferred port ranges or fallback to any available port if the preferred ones are occupied.
Getting an available port
This feature allows you to get an available port on the machine. If no arguments are passed, it returns any available port.
const getPort = require('get-port-please');
getPort().then(port => {
console.log(`Available port: ${port}`);
});
Specifying preferred ports
This feature enables specifying an array of preferred ports. The function will return the first available port from the specified list or any available port if none from the list are free.
const getPort = require('get-port-please');
getPort({ preferredPorts: [3000, 3001, 3002] }).then(port => {
console.log(`Available port: ${port}`);
});
Specifying port ranges
This feature allows specifying ranges of ports to check for availability. It's useful when you need a port within a specific range.
const getPort = require('get-port-please');
getPort({ portRanges: [{ from: 8000, to: 8005 }] }).then(port => {
console.log(`Available port within range: ${port}`);
});
Similar to `get-port-please`, `portfinder` provides a way to get an available port on the host machine. However, `portfinder` focuses more on finding an open port by incrementing from a base port, rather than specifying preferred ports or ranges.
The `get-port` package also helps in finding an available port. While it shares similar basic functionality with `get-port-please`, it does not offer the same level of control over specifying port ranges or preferred port lists.
Readme
Get an available TCP port to listen
Install package:
npm i get-port-please
// ESM
import {
getPort,
checkPort,
getRandomPort,
waitForPort,
} from "get-port-please";
// CommonJS
const {
getPort,
checkPort,
getRandomPort,
waitForPort,
} = 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;
}
name
Unique name for port memorizing. Default is default
.
random
If enabled, port
and ports
will be ignored. Default is false
.
port
First port to check. Default is process.env.PORT || 3000
ports
Extended ports to check.
portRange
Extended 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.
alternativePortRange
Alternative 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).
host
The host to check. Default is process.env.HOST
otherwise all available hosts will be checked.
MIT
FAQs
Get an available TCP port to listen
The npm package get-port-please receives a total of 1,050,163 weekly downloads. As such, get-port-please popularity was classified as popular.
We found that get-port-please demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.