inject a global variable into a module require() tree
Last updated 3 years ago by goto-bus-stop .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install require-with-global 
SYNC missed versions from official npm registry.


inject a global variable into a module require() tree

npm travis standard


npm install require-with-global


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


r.remove() // Remove the require hook


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.


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.



Current Tags

  • 2.0.1                                ...           latest (3 years ago)

3 Versions

  • 2.0.1                                ...           3 years ago
  • 2.0.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 2
Dependencies (6)
Dev Dependencies (3)

Copyright 2014 - 2017 © |