@funboxteam/free-port-finder

中文文档
4.0.0发布于 2023/06/06MITCommonJS零依赖近 30 天下载 2796

Tiny utility for checking ports availability

原始使用文档
文档目录4 节
  1. Rationale
  2. Installation
  3. Usage
  4. How it works

@funboxteam/free-port-finder

npm

The package checks availability of the passed port, or tries to find the first available port starting from the passed one.

По-русски

Rationale

When you start a TCP (or HTTP) server on localhost you want to be sure that the desirable port isn't occupied or you don't really care and want to get the first available one.

This tiny tool takes on this task.

Installation

npm install --save-dev @funboxteam/free-port-finder

Usage

findFreePort(port: number, host?: string): Promise<number> searches for the available port starting from the passed one:

const { findFreePort } = require('@funboxteam/free-port-finder');

const port = 3000;

findFreePort(port)
  .then(freePort => {
    config.devServer.port = freePort;
  });

isPortFree(port: number, host?: string): Promise<bool> checks the availability of the passed port:

const { isPortFree } = require('@funboxteam/free-port-finder');

const port = 3000;

isPortFree(port)
  .then(isFree => {
    if (isFree) {
      // start
    } else {
      // cancel
    }
  });

It is possible to pass host value as second parameter. The default host value is '0.0.0.0'.

To check if port on specified host is available:

const { isPortFree } = require('@funboxteam/free-port-finder');

const port = 3000;
const host = '127.0.0.1';

isPortFree(port, host)
  .then(isFree => {
    if (isFree) {
      // start
    } else {
      // cancel
    }
  });

How it works

In order to understand whether the port is free or not, this package tries to run a TCP-server on the specified port. If the server starts, then package kills it and signals that the port is free. If an EADDRINUSE error occurs, it signals that the port is busy. If any other error occurs, then package throws it out.

This solution has both pros and cons.

Cons:
In some OS incorrect SO_REUSEADDR processing is possible, and a free port can be signaled as busy (but not vice versa).

Pros:
It would be possible to go the other way, and try to connect to sockets at a specified address, rather than run server on them. But in this case, it is possible that the connection to a socket may initiate some process in its handler, which could potentially be irreversible. Therefore, current solution is safe.

Sponsored by FunBox

版本与发布记录

@funboxteam/free-port-finder 共发布 4 个版本,首次发布于 2020/07/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.0latest2023/06/063 年前
3.1.12021/06/105 年前
3.1.02020/08/106 年前
3.0.02020/07/166 年前

关于 @funboxteam/free-port-finder 的常见问题

@funboxteam/free-port-finder 是什么?

Tiny utility for checking ports availability

如何安装 @funboxteam/free-port-finder?

在项目目录运行 npm install @funboxteam/free-port-finder,或使用 pnpm add / yarn add / bun add。

@funboxteam/free-port-finder 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/funboxteam__free-port-finder 是否存在。

@funboxteam/free-port-finder 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@funboxteam/free-port-finder 可以商用吗?

@funboxteam/free-port-finder 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@funboxteam/free-port-finder 的下载量有多少?

根据 npm 官方下载统计,@funboxteam/free-port-finder 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,796 次,日均约 93 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@funboxteam/free-port-finder 还在维护吗?

最新版本 4.0.0 发布于 2023/06/06(3 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/07/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。