wait-port: Network Port Availability Utility

1.1.0 · active · verified Sun Apr 19

wait-port is a utility for Node.js environments designed to asynchronously wait for a network port, HTTP endpoint, or DNS record to become available. It is particularly useful in scripting scenarios, such as `docker-compose` setups, CI/CD pipelines, or application startup scripts, where service dependencies need to be met before proceeding. The package provides both a command-line interface (CLI) and a programmatic API. The current stable version is 1.1.0, released in September 2023, indicating an active development and maintenance cadence. Key features include configurable timeouts, support for TCP, HTTP (checking for 200-class status codes), and DNS resolution (`ENOTFOUND` handling), and robust IPv4/IPv6 support, including 'happy eyeballs' via `autoSelectFamily` introduced in v1.1.0. It distinguishes itself by its simplicity and dual usage model, making it a reliable choice for ensuring service readiness.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic use of `wait-port` to check for a port's availability with a timeout and handles both success and failure.

import waitPort from 'wait-port';

async function waitForMyService() {
  console.log('Attempting to connect to example.com:443...');
  try {
    const params = {
      host: 'example.com',
      port: 443,
      timeout: 15000, // 15 seconds timeout
      output: 'dots' // Show progress dots
    };

    const { open, ipVersion } = await waitPort(params);

    if (open) {
      console.log(`Successfully connected to example.com:443 on IPv${ipVersion}!`);
      // Proceed with application logic
    } else {
      console.log('Failed to connect to example.com:443 within the timeout period.');
      process.exit(1);
    }
  } catch (error) {
    console.error(`An unexpected error occurred: ${error.message}`);
    process.exit(2);
  }
}

waitForMyService();

view raw JSON →