CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-portfinder

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

82

1.64x
Overview
Eval results
Files

port-configuration.mddocs/

Port Configuration

Global configuration settings for portfinder operations including base port, highest port, and socket path defaults.

Capabilities

Base Port Property

The lowest port to begin any port search from. Defaults to 8000.

/**
 * The lowest port to begin any port search from
 * @type {number}
 * @default 8000
 */
portfinder.basePort: number;

Usage Example:

const portfinder = require('portfinder');

// Check current base port
console.log('Current base port:', portfinder.basePort); // 8000

// Modify base port globally
portfinder.basePort = 3000;

// All subsequent port searches will start from 3000
portfinder.getPortPromise().then(port => {
  console.log('Port found:', port); // e.g., 3000 or higher
});

Set Base Port Function

Sets the lowest port to begin any port search from.

/**
 * Set the lowest port to begin any port search from
 * @param {number} port - The new base port
 * @returns {void}
 */
function setBasePort(port: number): void;

Usage Example:

const portfinder = require('portfinder');

// Set base port using function
portfinder.setBasePort(5000);

console.log('Base port is now:', portfinder.basePort); // 5000

Highest Port Property

The highest port to end any port search from. Defaults to 65535 (maximum possible port).

/**
 * The highest port to end any port search from
 * @type {number}
 * @default 65535
 */
portfinder.highestPort: number;

Usage Example:

const portfinder = require('portfinder');

// Check current highest port
console.log('Current highest port:', portfinder.highestPort); // 65535

// Limit search range
portfinder.highestPort = 9000;

// Port searches will not exceed 9000
portfinder.getPort({ port: 8500 }, (err, port) => {
  console.log('Port found in limited range:', port); // between 8500-9000
});

Set Highest Port Function

Sets the highest port to end any port search from.

/**
 * Set the highest port to end any port search from
 * @param {number} port - The new highest port
 * @returns {void}
 */
function setHighestPort(port: number): void;

Usage Example:

const portfinder = require('portfinder');

// Set highest port using function
portfinder.setHighestPort(8080);

console.log('Highest port is now:', portfinder.highestPort); // 8080

Base Path Property

Default path to begin any socket search from. Defaults to '/tmp/portfinder'.

/**
 * Default path to begin any socket search from
 * @type {string}
 * @default '/tmp/portfinder'
 */
portfinder.basePath: string;

Usage Example:

const portfinder = require('portfinder');

// Check current base path
console.log('Current base path:', portfinder.basePath); // '/tmp/portfinder'

// Change base path
portfinder.basePath = '/var/run/myapp';

// Socket searches will use new base path
portfinder.getSocketPromise().then(socketPath => {
  console.log('Socket path:', socketPath); // e.g., '/var/run/myapp.sock'
});

Set Base Path Function

Sets the base path to begin any socket search from.

/**
 * Set the base path to begin any socket search from
 * @param {string} path - The new base path
 * @returns {void}
 */
function setBasePath(path: string): void;

Usage Example:

const portfinder = require('portfinder');

// Set base path using function
portfinder.setBasePath('/tmp/sockets');

console.log('Base path is now:', portfinder.basePath); // '/tmp/sockets'

Default Hosts Array

Internal list of hostnames and IP addresses derived from network interfaces. This array is automatically populated and includes '0.0.0.0', all local interface addresses, and null (for binding without specifying a host).

/**
 * List of internal hostnames provided by your machine
 * @type {string[]}
 * @readonly
 */
portfinder._defaultHosts: string[];

Usage Example:

const portfinder = require('portfinder');

// Inspect available hosts (read-only)
console.log('Available hosts:', portfinder._defaultHosts);
// Example output: ['0.0.0.0', '::1', '127.0.0.1', '192.168.1.100', null]

// Custom hosts are automatically added when used
portfinder.getPort({ host: '192.168.1.50' }, (err, port) => {
  console.log('Updated hosts:', portfinder._defaultHosts);
  // '192.168.1.50' is now included in the array
});

Configuration Best Practices

Global vs Per-Call Configuration

const portfinder = require('portfinder');

// Global configuration affects all subsequent calls
portfinder.setBasePort(3000);
portfinder.setHighestPort(4000);

// Per-call options override global settings
portfinder.getPort({
  port: 5000,     // Overrides global basePort
  stopPort: 6000  // Overrides global highestPort
}, callback);

// Global settings remain unchanged for other calls
portfinder.getPortPromise(); // Still uses basePort=3000, highestPort=4000

Development vs Production Settings

const portfinder = require('portfinder');

if (process.env.NODE_ENV === 'development') {
  // Use standard development port range
  portfinder.setBasePort(3000);
  portfinder.setHighestPort(3999);
} else {
  // Use higher port range for production
  portfinder.setBasePort(8000);
  portfinder.setHighestPort(9999);
}

Install with Tessl CLI

npx tessl i tessl/npm-portfinder

docs

index.md

port-configuration.md

port-finding.md

socket-finding.md

tile.json