# @eggjs/ip 中文文档

规范地址：https://npmcn.com/package/%40eggjs/ip/

版本：2\.1\.0
npm 元数据更新时间：2026-06-30T15:34:24.009Z
中文译文更新时间：2026-11-13T08:59:46.836Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40eggjs/ip
原文对照：https://npmcn.com/package/%40eggjs/ip/?lang=original
项目主页：https://github.com/eggjs/node-ip
源码仓库：https://github.com/eggjs/node-ip

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：20.6 kB，4 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>= 14\.0\.0
- 运行依赖：0 个
- 首次发布：2024-06-12T08:13:17.811Z
- 最新版本发布：2025-01-13T12:57:54.522Z
- 近 30 天下载量：125904（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 0 个）

## 中文简介

\[!\] \[NPM版本 \[npm-image\] \[npm-url\] (英语) [![Node.js CI](https://github.com/eggjs/node-ip/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/node-ip/actions/workflows/nodejs.yml) ! \[试播 \[代码cov-image\] \[代码cov-url\] \[!\] \[知道Vulnerabil!\]

## 安装

```sh
npm install @eggjs/ip
```

也可以使用：`pnpm add @eggjs/ip`、`yarn add @eggjs/ip`、`bun add @eggjs/ip`

## README

# @eggjs/ip (英语)

[![NPM version](https://img.shields.io/npm/v/@eggjs/ip.svg?style=flat-square)](https://npmjs.org/package/@eggjs/ip) [![Node.js CI](https://github.com/eggjs/node-ip/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/node-ip/actions/workflows/nodejs.yml) [![Test coverage](https://codecov.io/gh/eggjs/node-ip/branch/master/graph/badge.svg)](https://codecov.io/gh/eggjs/node-ip) [![Known Vulnerabilities](https://snyk.io/test/npm/@eggjs/ip/badge.svg?style=flat-square)](https://snyk.io/test/npm/@eggjs/ip) [![npm download](https://img.shields.io/npm/dm/@eggjs/ip.svg?style=flat-square)](https://npmjs.org/package/@eggjs/ip)

节点的 IP 地址公用设施

安全修复叉, 合并 [https://github.com/indutny/node-ip/pull/144 页面存档备份,存于互联网档案馆](https://github.com/indutny/node-ip/pull/144)

## 安装

### 无

```shell
npm install @eggjs/ip
```

## 使用量

获取您的 ip 地址, 比较 ip 地址, 验证 ip 地址等 .

```js
var ip = require('@eggjs/ip');

ip.address() // my ip address
ip.isEqual('::1', '::0:1'); // true
ip.toBuffer('127.0.0.1') // Buffer([127, 0, 0, 1])
ip.toString(new Buffer([127, 0, 0, 1])) // 127.0.0.1
ip.fromPrefixLen(24) // 255.255.255.0
ip.mask('192.168.1.134', '255.255.255.0') // 192.168.1.0
ip.cidr('192.168.1.134/26') // 192.168.1.128
ip.not('255.255.255.0') // 0.0.0.255
ip.or('192.168.1.134', '0.0.0.255') // 192.168.1.255
ip.isPrivate('127.0.0.1') // true
ip.isV4Format('127.0.0.1'); // true
ip.isV6Format('::ffff:127.0.0.1'); // true
ip.isValid('127.0.0.1'); // true

// operate on buffers in-place
var buf = new Buffer(128);
var offset = 64;
ip.toBuffer('127.0.0.1', buf, offset);  // [127, 0, 0, 1] at offset 64
ip.toString(buf, offset, 4);            // '127.0.0.1'

// subnet information
ip.subnet('192.168.1.134', '255.255.255.192')
// { networkAddress: '192.168.1.128',
//   firstAddress: '192.168.1.129',
//   lastAddress: '192.168.1.190',
//   broadcastAddress: '192.168.1.191',
//   subnetMask: '255.255.255.192',
//   subnetMaskLength: 26,
//   numHosts: 62,
//   length: 64,
//   contains: function(addr){...} }
ip.cidrSubnet('192.168.1.134/26')
// Same as previous.

// range checking
ip.cidrSubnet('192.168.1.134/26').contains('192.168.1.190') // true

// ipv4 long conversion
ip.toLong('127.0.0.1'); // 2130706433
ip.fromLong(2130706433); // '127.0.0.1'

// malformed addresses and normalization
ip.normalizeStrict('0::01'); // '::1'
ip.isPrivate('0x7f.1'); // throw error
ip.isValidAndPrivate('0x7f.1'); // false
ip.normalizeStrict('0x7f.1'); // throw error
var normalized = ip.normalizeLax('0x7f.1'); // 127.0.0.1
ip.isPrivate(normalized); // true
```

## 贡献者

[![Contributors](https://contrib.rocks/image?repo=eggjs/node-ip)](https://github.com/eggjs/node-ip/graphs/contributors)

制造的 [贡献者-img](https://contrib.rocks/)。 。 。 .

### 许可证

```txt
This software is licensed under the MIT License.

Copyright (c) 2024-present eggjs and other contributors.
Copyright Fedor Indutny, 2012.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
```

## 常见问题

### @eggjs/ip 是什么？

\[\!\] \[NPM版本 \[npm-image\] \[npm-url\] \(英语\) \! \[试播 \[代码cov-image\] \[代码cov-url\] \[\!\] \[知道Vulnerabil\!\]

### 如何安装 @eggjs/ip？

在项目目录运行 npm install @eggjs/ip，或使用 pnpm add / yarn add / bun add。

### @eggjs/ip 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @eggjs/ip 支持 ESM 还是 CommonJS？

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

### @eggjs/ip 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>= 14\.0\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @eggjs/ip 可以商用吗？

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

### @eggjs/ip 的下载量有多少？

根据 npm 官方下载统计，@eggjs/ip 在 2026/10/13 – 2026/11/11 这 30 天内被下载 125,904 次，日均约 4,197 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @eggjs/ip 还在维护吗？

最新版本 2\.1\.0 发布于 2025/01/13（1\.8 年前）；至今共发布 2 个版本，其中过去 12 个月发布了 0 个；首次发布于 2024/06/12。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

