# require-in-the-middle

> Module to hook into the Node.js require function

Latest version **8.0.1** (published 2025-10-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install require-in-the-middle
pnpm add require-in-the-middle
yarn add require-in-the-middle
bun add require-in-the-middle
```

## Health

**Score 65/100 (B)** — status: stable.

Positive: has types; no vulnerabilities; has provenance; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 8.0.1 |
| Published | 2025-10-20 |
| First published | 2016-02-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=9.3.0 \|\| >=8.10.0 <9.0.0 |
| Dependencies | 2 |
| Unpacked size | 17.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 186 |
| Author | Thomas Watson Steen |
| Maintainers | trentm, watson, nodejs-foundation |
| Keywords | require, hook, shim, shimmer, shimming, patch, monkey, monkeypatch, module, load |

## Links

- npm: https://www.npmjs.com/package/require-in-the-middle
- Repository: https://github.com/nodejs/require-in-the-middle
- Homepage: https://github.com/nodejs/require-in-the-middle#readme
- Issues: https://github.com/nodejs/require-in-the-middle/issues
- npm.io page: https://npm.io/package/require-in-the-middle

## Dependencies (2)

- [debug](https://npm.io/package/debug.md) ^4.3.5
- [module-details-from-path](https://npm.io/package/module-details-from-path.md) ^1.0.3

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 8.0.1 (latest) — 2025-10-20
- 8.0.0 — 2025-09-24
- 7.5.2 — 2025-02-21
- 7.5.1 — 2025-02-07
- 7.5.0 — 2025-01-27
- 7.4.0 — 2024-07-25
- 7.3.0 — 2024-03-26
- 7.2.1 — 2024-03-13
- 7.2.0 — 2023-07-13
- 7.1.1 — 2023-05-31
- 7.1.0 — 2023-04-19
- 7.0.1 — 2023-04-13
- 7.0.0 — 2023-04-13
- 6.0.0 — 2023-01-13
- 5.2.0 — 2022-08-12
- … 20 more at https://npm.io/package/require-in-the-middle/versions

## README

# require-in-the-middle

Hook into the Node.js `require` function. This allows you to modify
modules on-the-fly as they are being required.

[![npm](https://img.shields.io/npm/v/require-in-the-middle.svg)](https://www.npmjs.com/package/require-in-the-middle)
[![Test status](https://github.com/nodejs/require-in-the-middle/workflows/Test/badge.svg)](https://github.com/nodejs/require-in-the-middle/actions)

Also supports hooking into calls to `process.getBuiltinModule()`, which was introduced in Node.js v22.3.0.

## Installation

```
npm install require-in-the-middle --save
```

## Usage

```js
const path = require('path')
const { Hook } = require('require-in-the-middle')

// Hook into the express and mongodb module
new Hook(['express', 'mongodb'], function (exports, name, basedir) {
  const version = require(path.join(basedir, 'package.json')).version

  console.log('loading %s@%s', name, version)

  // expose the module version as a property on its exports object
  exports._version = version

  // whatever you return will be returned by `require`
  return exports
})
```

## API

The require-in-the-middle module exposes a single function:

### `hook = new Hook([modules][, options], onrequire)`

When called a `hook` object is returned.

Arguments:

- `modules` &lt;string[]> An optional array of module names to limit which modules
  trigger a call of the `onrequire` callback. If specified, this must be the
  first argument. Both regular modules (e.g. `react-dom`) and
  sub-modules (e.g. `react-dom/server`) can be specified in the array.
- `options` &lt;Object> An optional object containing fields that change when the
  `onrequire` callback is called. If specified, this must be the second
  argument.
  - `options.internals` &lt;boolean> Specifies whether `onrequire` should be called
    when module-internal files are loaded; defaults to `false`.
- `onrequire` &lt;Function> The function to call when a module is required.

The `onrequire` callback will be called the first time a module is
required. The function is called with three arguments:

- `exports` &lt;Object> The value of the `module.exports` property that would
  normally be exposed by the required module.
- `name` &lt;string> The name of the module being required. If `options.internals`
  was set to `true`, the path of module-internal files that are loaded
  (relative to `basedir`) will be appended to the module name, separated by
  `path.sep`.
- `basedir` &lt;string> The directory where the module is located, or `undefined`
  for core modules.

Return the value you want the module to expose (normally the `exports`
argument).

### `hook.unhook()`

Removes the `onrequire` callback so that it will not be triggerd by
subsequent calls to `require()` or `process.getBuiltinModule()`.

## License

[MIT](https://github.com/nodejs/require-in-the-middle/blob/master/LICENSE)

---
_Source: https://npm.io/package/require-in-the-middle · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
