1. get-port-please
Get an available TCP port to listen
get-port-please
Package: get-port-please
Created by: unjs
Last modified: Tue, 09 Jan 2024 12:01:48 GMT
Version: 3.1.2
License: MIT
Downloads: 5,815,513
Repository: https://github.com/unjs/get-port-please

Install

npm install get-port-please
yarn add get-port-please

🔌 get-port-please

Get an available TCP port to listen

npm version
npm downloads
License
JSDocs

Usage

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

Options

 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.

License

MIT

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin