pirates

中文文档
4.0.7发布于 2025/03/27MITTypeScriptCommonJS零依赖近 30 天下载 3.2亿

Properly hijack require, i.e., properly define require hooks and customizations

原始使用文档
文档目录7 节
  1. Properly hijack require
  2. Why?
  3. Installation
  4. Usage
  5. API
  6. pirates.addHook(hook, [opts={ [matcher: true], [exts: ['.js…
  7. Projects that use Pirates

Pirates Coverage

Properly hijack require

This library allows to add custom require hooks, which do not interfere with other require hooks.

This library only works with commonJS.

Why?

Two reasons:

  1. Babel and istanbul were breaking each other.
  2. Everyone seemed to re-invent the wheel on this, and everyone wanted a solution that was DRY, simple, easy to use, and made everything Just Work™, while allowing multiple require hooks, in a fashion similar to calling super.

For some context, see the Babel issue thread which started this all, then the nyc issue thread, where discussion was moved (as we began to discuss just using the code nyc had developed), and finally to #1 where discussion was finally moved.

Installation

npm install --save pirates

Usage

Using pirates is really easy:

// my-module/register.js
const addHook = require('pirates').addHook;
// Or if you use ES modules
// import { addHook } from 'pirates';

function matcher(filename) {
  // Here, you can inspect the filename to determine if it should be hooked or
  // not. Just return a truthy/falsey. Files in node_modules are automatically ignored,
  // unless otherwise specified in options (see below).

  // TODO: Implement your logic here
  return true;
}

const revert = addHook(
  (code, filename) => code.replace('@@foo', 'console.log(\'foo\');'),
  { exts: ['.js'], matcher }
);

// And later, if you want to un-hook require, you can just do:
revert();

API

pirates.addHook(hook, [opts={ [matcher: true], [exts: ['.js']], [ignoreNodeModules: true] }]);

Add a require hook. hook must be a function that takes (code, filename), and returns the modified code. opts is an optional options object. Available options are: matcher, which is a function that accepts a filename, and returns a truthy value if the file should be hooked (defaults to a function that always returns true), falsey if otherwise; exts, which is an array of extensions to hook, they should begin with . (defaults to ['.js']); ignoreNodeModules, if true, any file in a node_modules folder wont be hooked (the matcher also wont be called), if false, then the matcher will be called for any files in node_modules (defaults to true).

Projects that use Pirates

See the wiki page. If you add Pirates to your project, (And you should! It works best if everyone uses it. Then we can have a happy world full of happy require hooks!), please add yourself to the wiki.

版本与发布记录

pirates 共发布 20 个版本,首次发布于 2015/12/06;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.7latest2025/03/271.6 年前
4.0.62023/06/203 年前
4.0.52022/01/244 年前
4.0.42021/12/084 年前
4.0.32021/12/074 年前
4.0.22021/12/074 年前

关于 pirates 的常见问题

pirates 是什么?

Properly hijack require, i.e., properly define require hooks and customizations

如何安装 pirates?

在项目目录运行 npm install pirates,或使用 pnpm add / yarn add / bun add。

pirates 支持 TypeScript 吗?

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

pirates 支持 ESM 还是 CommonJS?

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

pirates 需要什么版本的 Node.js?

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

pirates 可以商用吗?

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

pirates 的下载量有多少?

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

pirates 还在维护吗?

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