Socket
Socket
Sign inDemoInstall

portfinder

Package Overview
Dependencies
6
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    portfinder

A simple tool to find an open port on the current machine


Version published
Weekly downloads
7.4M
increased by3.55%
Maintainers
2
Install size
1.98 MB
Created
Weekly downloads
 

Package description

What is portfinder?

The portfinder npm package is a utility for getting a port number that is guaranteed to be free on the current machine. It is useful for running servers on dynamically assigned ports to avoid conflicts.

What are portfinder's main functionalities?

Get an available port

This feature allows you to find an available port on the machine. The `getPort` function takes a callback that provides the port number.

const portfinder = require('portfinder');

portfinder.getPort((err, port) => {
  if (err) {
    throw err;
  }
  console.log('Available port found:', port);
});

Set the base port

Before calling `getPort`, you can set the `basePort` property to tell portfinder to start searching for a free port from a certain number.

const portfinder = require('portfinder');

portfinder.basePort = 3000;
portfinder.getPort((err, port) => {
  if (err) {
    throw err;
  }
  console.log('Available port found:', port);
});

Set the range of ports to search

You can specify a range of ports for portfinder to search within by providing an options object with `port` and `stopPort` properties.

const portfinder = require('portfinder');

portfinder.getPort({
  port: 3000, // minimum port
  stopPort: 3333 // maximum port
}, (err, port) => {
  if (err) {
    throw err;
  }
  console.log('Available port found:', port);
});

Other packages similar to portfinder

Readme

Source

node-portfinder CI

Installation

  $ npm install portfinder

Usage

The portfinder module has a simple interface:

  var portfinder = require('portfinder');

  portfinder.getPort(function (err, port) {
    //
    // `port` is guaranteed to be a free port
    // in this scope.
    //
  });

Or with promise (if Promises are supported) :

  const portfinder = require('portfinder');

  portfinder.getPortPromise()
    .then((port) => {
        //
        // `port` is guaranteed to be a free port
        // in this scope.
        //
    })
    .catch((err) => {
        //
        // Could not get a free port, `err` contains the reason.
        //
    });

If portfinder.getPortPromise() is called on a Node version without Promise (<4), it will throw an Error unless Bluebird or any Promise pollyfill is used.

Ports search scope

By default portfinder will start searching from 8000 and scan until maximum port number (65535) is reached.

You can change this globally by setting:

portfinder.setBasePort(3000);    // default: 8000
portfinder.setHighestPort(3333); // default: 65535

or by passing optional options object on each invocation:

portfinder.getPort({
    port: 3000,    // minimum port
    stopPort: 3333 // maximum port
}, callback);

Run Tests

  $ npm test
Author: Charlie Robbins
Author/Maintainer: Erik Trom
License: MIT/X11

Keywords

FAQs

Last updated on 13 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