JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10312274
  • Score
    100M100P100Q237418F
  • License MIT

Module to hook into the Node.js require function

Package Exports

  • require-in-the-middle

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (require-in-the-middle) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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.

Build status js-standard-style

Installation

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

Usage

var path = require('path')
var hook = require('require-in-the-middle')

// Hook into the express and mongodb module
hook(['express', 'mongodb'], function (exports, name, basedir) {
  var 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:

function ([modules, ]onrequire) {}

You can optionally supply an array of module names as the first argument to limit which modules will trigger a call of the onrequire callback.

Supply a callback function as the last argument. This function will be called the first time a module is required. The onrequire function is called with three arguments:

  • exports - The value of the module.exports property that would normally be exposed by the required module
  • name - The name of the module being required
  • basedir - The directory of the where the module is located (will be undefined if core module)

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

License

MIT