Socket
Socket
Sign inDemoInstall

get-port-please

Package Overview
Dependencies
1
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    get-port-please

Get an available TCP port to listen


Version published
Maintainers
1
Install size
21.7 kB
Created

Package description

What is get-port-please?

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.

What are get-port-please's main functionalities?

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}`);
});

Other packages similar to get-port-please

Changelog

Source

2.6.1 (2022-08-08)

Bug Fixes

  • ignore falsy config.port option (ca34365)

Readme

Source

get-port-please

get-port-please

Get an available TCP port to listen

npm npm (scoped with tag)

Usage

Install package:

yarn add get-port-please
# or
npm install 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 > memo > random

Options

interface GetPortOptions {
  name?: string

  random?: boolean
  port?: number
  portRange?: [from: number, to: 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

Alternative ports to check.

portRange

Alternative port range to check. Default is [3000, 3100]

host

The host to check. Default is process.env.HOST otherwise all available hosts will be checked.

memoDir / memoName

Options passed to fs-memo

  • Default dir: node_modules/get-port/dist
  • Default name: .get-port

License

MIT

FAQs

Last updated on 08 Aug 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc