Hook into the Node.js require function. This allows you to modify
modules on-the-fly as they are being required.
npm install require-in-the-middle --save
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
})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 themodule.exportsproperty that would normally be exposed by the required modulename- The name of the module being requiredbasedir- The directory of the where the module is located (will beundefinedif core module)
Return the value you want the module to expose (normally the exports
argument).
MIT