7.3.0 • Published 2 days ago

require-in-the-middle v7.3.0

Weekly downloads
1,673,376
License
MIT
Repository
github
Last release
2 days ago

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

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().

License

MIT

@drivetribe/dd-trace@infinitebrahmanuniverse/nolb-requi@everything-registry/sub-chunk-2654hotswap-modulehttp-telemetrykelvin-nodekelvin-instrumentationlatest_boltnextapmmysdkfornodetempv2metlols-traceelastic-apm-nodeelastic-apm-node-unofficialelastic-apm-node-with-metricselastic-apm-tediouselastic-apm-node-meteoreb-elastic-apm-nodeepsagonexpress-appenlightnpm-all-packagespx5open-apmpreload-font-pluginopbeatopbeat-multiagentotel-aws-sdk-retry-patch@sprkl/obs-jestpermitiopermitio-alphapermitio-serverlesspinpoint-node-agentpm2t-io-apmzuqa-agent-node@google-cloud/trace-agent@frielforreal/google-cloud-trace@hypertrace/nodejsagenttesthahathisisnotarealpackagetesthahathisisnotarealpackage2teth-sdk@inspector-apm/inspector-nodejs@knuckleswtf/scribe-express@knuckleswtf/scribe-restifytestbolttestboltnew@nmarks/dd-traceshieldfyshieldfy-nodejs-clientsails-hook-recap-devshimmingtestshimo-opentracing-autosignalfx-tracingraygun-apmraygun-node-common@mzahor-test-org/opentelemetry-node-fork@opencensus/nodejs-base@opentelemetry/shim-opencensus@mojaloop/elastic-apm-node@narwhal/utils@recap.dev/client@thomasbui93/dd-trace@thundra/core@uyu423/pinpoint-node-agent@undefinedlabs/scope-agent@threatstack/threatstack-agent-node@vitramir/elastic-apm-node@palzin-apm/palzin-nodejs@sidharthachatterjee/elastic-apm-node@opentelemetry/instrumentation@pm2/io@sprkl/obs@sprkl/obs-opentelemetry-instrumentation-jest@risingstack/opentracing-auto@scout_apm/scout-apm@tykevision/agent@securenative/agent@securenative/sdk@zaplabs/opencensus-nodejs-base@yamadayuki/nodejs-base@yaguara/dd-trace@zcong/prometheus-instrumentation-grpcauthorizon@antongolub/multi-semantic-release@arextest/arex-node-agent@aspecto/coreatatus-nodejs@aztec/multi-semantic-releasebitsensor-node-js-corec3d-opentracing-auto@catchpoint/traceboltbeta@danielkoehler/nodejs@deal/opencensus-nodejs@codeparrot/instrumentation@cran/gql.jm@cran/lib.gql.jmcra-preactcyberchief-bolt@doubret/opentracing-auto@doubret/tracing
7.3.0

2 days ago

7.2.1

16 days ago

7.2.0

9 months ago

7.1.1

10 months ago

7.0.0

12 months ago

7.1.0

11 months ago

7.0.1

12 months ago

6.0.0

1 year ago

5.2.0

2 years ago

5.1.0

3 years ago

5.0.3

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.1.0

6 years ago

3.0.0

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.2

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.0

8 years ago