require-with-global v2.0.1
require-with-global
inject a global variable into a module require() tree
Install
npm install require-with-globalUsage
var withGlobal = require('require-with-global')
var uppercaseStream = require('through2')(function (chunk, enc, next) {
next(null, chunk.toUpperCase())
})
var virtualConsole = require('console').Console(uppercaseStream)
var r = withGlobal()
// `console` in ./app.js and all child modules will be `virtualConsole`.
var app = r('./app', {
console: virtualConsole
})
app.run()
uppercaseStream.pipe(process.stdout)
r.remove() // Remove the require hookAPI
req = withGlobal()
Create a require function that can be used to inject module-global variables.
req(specifier, vars)
Require the module at specifier, injecting vars into all its deeply required
modules. All the variables specified in the vars object will be available in
modules required using the req function. Calls to require() inside the
module will be wrapped so that vars are also recursively injected in
dependencies of the module.
req.remove()
Remove the compile hooks and global state that withGlobal uses to inject
variables into modules.
Note that globals that were already injected will continue to work; it's
perfectly safe to call req.remove() immediately after requiring some modules,
if you are certain that there are no lazy require() calls left.