Detalhes do pacote

require-with-global

goto-bus-stop36Apache-2.02.0.1

inject a global variable into a module require() tree

global, inject, insert, mock

readme (leia-me)

require-with-global

inject a global variable into a module require() tree

npm travis standard

Install

npm install require-with-global

Usage

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 hook

API

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.

License

Apache-2.0

changelog (log de mudanças)

require-with-global change log

All notable changes to this project will be documented in this file.

This project adheres to Semantic Versioning.

2.0.1

  • Don't hook builtin modules, fixes a problem where require-with-global would prefer modules installed in node_modules over builtins.

2.0.0

  • Make withGlobal() return a require()-like function that accepts global definitions in its second paramater, so it can be reused for different injections.

1.0.0

  • Initial release.