Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

get-port-please

unjs5.3mMIT3.1.2TypeScript support: included

Get an available TCP port to listen

readme

🔌 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

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v3.1.2

compare changes

🩹 Fixes

  • Accept ipv6 as valid hostname (d537a51)
  • Use closed ranges as port ranges (#66)
  • Ranges now work even if port is unset in options (#72)
  • Don't use random port by default if user specified any port (#65)

💅 Refactors

  • Correct typos in GetPortError message (#77)
  • Fix typo in error message (#73)
  • Improve log when using alternative port (#78)

🏡 Chore

  • Update fallback message (1f45050)
  • Update dependencies and lockfile (62735f6)

✅ Tests

❤️ Contributors

v3.1.1

compare changes

🩹 Fixes

  • Ignore ipv6 link-local addresses from hosts (9e76e76)

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.1.0

compare changes

🚀 Enhancements

  • Automatically fallback hostname for invalid hosts (#62)

🩹 Fixes

  • Validate hostname and improve errors (#59)
  • Skip all listen errors (#61)

💅 Refactors

  • Split internals and types (cf4317c)

🏡 Chore

  • Run tests on windows (#60)
  • Use changelogen for releases (7d4d6bb)
  • release: V3.0.3 (86672c4)

✅ Tests

  • Block host on all interfaces (5a95184)
  • Skip invalid host tests on windows (e8b92ac)

🎨 Styles

❤️ Contributors

  • Pooya Parsa (@pi0)

v3.0.3

compare changes

🩹 Fixes

  • Validate hostname and improve errors (#59)
  • Skip all listen errors (#61)

💅 Refactors

  • Split internals and types (cf4317c)

🏡 Chore

  • Run tests on windows (#60)
  • Use changelogen for releases (7d4d6bb)

✅ Tests

  • Block host on all interfaces (5a95184)
  • Skip invalid host tests on windows (e8b92ac)

🎨 Styles

❤️ Contributors

  • Pooya Parsa (@pi0)

3.0.2 (2023-08-27)

Bug Fixes

  • use random port when port: 0 is set (081ed99)

3.0.1 (2023-01-03)

Bug Fixes

  • use process.env.PORT as default value if port option is missing (#27) (57e1232)

3.0.0 (2022-11-14)

⚠ BREAKING CHANGES

  • no default value for alternativePortRange if explicit port provided
  • drop memo support

Bug Fixes

  • no default value for alternativePortRange if explicit port provided (00366ae), closes #15

2.6.1 (2022-08-08)

Bug Fixes

  • ignore falsy config.port option (ca34365)

2.6.0 (2022-08-08)

Features

  • alternative port and verbose logging (49200e6)
  • verbose logging (opt-in) (779f3b7)

Bug Fixes

  • generate range before passing to findPort (848a8c2)

2.5.0 (2022-04-11)

Features

2.4.3 (2022-02-28)

Bug Fixes

  • waitForPort: check for all available hosts by default (ba5f3b0)

2.4.2 (2022-02-25)

Bug Fixes

  • waitForPort: wait for delay (01497a8)

2.4.1 (2022-02-25)

Bug Fixes

  • getRandomPort: host argument is optional (7081383)

2.4.0 (2022-02-25)

Features

  • check port against all available hosts (bac49cc)
  • export waitForPort (1c42832)

2.3.0 (2022-02-10)

Features

2.2.0 (2021-04-12)

Features

Bug Fixes

  • resolve defaults at getPort (dcab479)

2.1.0 (2020-12-04)

Features

  • allow config to be string or number (13f4275)

2.0.0 (2020-12-04)

⚠ BREAKING CHANGES

  • use named exports

Features

1.1.0 (2020-11-16)

Features

  • update fs-memo and improve config handling (5e4acee)

1.0.0 (2020-06-16)

0.0.6 (2020-06-01)

0.0.5 (2020-06-01)

Bug Fixes

0.0.4 (2020-06-01)

Features

  • name and random options (ccea688)

0.0.3 (2020-06-01)

Bug Fixes

  • types: use interface instead of type infering (09135eb)

0.0.2 (2020-06-01)

0.0.1 (2020-05-31)

Bug Fixes