Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

require-in-the-middle

elastic25.6mMIT7.4.0TypeScript support: included

Module to hook into the Node.js require function

require, hook, shim, shimmer, shimming, patch, monkey, monkeypatch, module, load

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 Test status

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

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 <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 <Object> An optional object containing fields that change when the onrequire callback is called. If specified, this must be the second argument.
    • options.internals <boolean> Specifies whether onrequire should be called when module-internal files are loaded; defaults to false.
  • onrequire <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 <Object> The value of the module.exports property that would normally be exposed by the required module.
  • name <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 <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

changelog

require-in-the-middle changelog

v7.4.0

v7.3.0

v7.2.1

v7.2.0

v7.1.1

  • Fallback to caching hooked module exports internally if the required module does not appear in require.cache. This can only happen if there is some other require hook in play -- e.g. @babel/register which is used by @babel/cli, aka the babel-node command. (https://github.com/elastic/require-in-the-middle/issues/72)

v7.1.0

v7.0.1

  • Fix hooking of 'http2' with Node.js versions [8.0, 8.8) where the 'http2' built-in module was behind the --expose-http2 flag. (https://github.com/elastic/require-in-the-middle/pull/68) Release v7.0.0 introduced a bug with this case where the process would crash with:

      AssertionError [ERR_ASSERTION]: unexpected that there is no Module entry for "http2" in require.cache
        at ExportsCache.set (.../require-in-the-middle4/index.js:72:7)

v7.0.0

  • Change the suggested require usage to be a Hook field on the exports,

    const { Hook } = require('require-in-the-middle');  // the new suggested way

    rather than the default export:

    const Hook = require('require-in-the-middle');  // deprecated, still supported for backward compat

    This is to avoid the need for users to use a default export which can get confusing or problematic with TypeScript. See https://github.com/open-telemetry/opentelemetry-js/issues/3701 for some details.

  • Change the suggested usage to new Hook(...) instead of Hook(...), but both are supported.

  • Use the Node.js require.cache for caching the exports returned from a Hook's onrequire. This allows users to delete entries from require.cache to trigger a re-load (and re-run of the hook's onrequire) of a module the next time it is required -- as mentioned at https://nodejs.org/docs/latest/api/all.html#all_modules_requirecache (https://github.com/elastic/require-in-the-middle/issues/61)

  • (SEMVER-MAJOR) Remove the hook.cache field. In earlier versions this was available and some tests used it. However it was never a documented field.

  • If resolving the filename for a require(...) fails, defer to the wrapped require implementation rather than failing right away. This allows a possibly-monkey-patched require to do its own special thing. (https://github.com/elastic/require-in-the-middle/pull/59)

v6.0.0

  • Drop Node.js 6 support. New minimum supported Node.js version is 8.6.0. (This is the minimum supported Node.js version for elastic-apm-node@3 that uses this module.)
  • Add testing of Node.js 19.
  • Rename default branch from master to main.
  • Should there be a need to do 5.x maintenance releases there is a 5.x branch.

v5.2.0

v5.1.0

earlier versions

Use the source, Luke.