# require-with-global

> inject a global variable into a module require() tree

Latest version **2.0.1** (published 2018-04-25) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install require-with-global
pnpm add require-with-global
yarn add require-with-global
bun add require-with-global
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2018-04-25 |
| First published | 2018-04-18 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 7.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Renée Kooi |
| Maintainers | goto-bus-stop |
| Keywords | global, inject, insert, mock, require, variable |

## Links

- npm: https://www.npmjs.com/package/require-with-global
- Repository: https://github.com/goto-bus-stop/require-with-global
- Issues: https://github.com/goto-bus-stop/require-with-global/issues
- npm.io page: https://npm.io/package/require-with-global

## Dependencies (6)

- [debug](https://npm.io/package/debug.md) ^3.1.0
- [assert](https://npm.io/package/assert.md) ^1.4.1
- [pirates](https://npm.io/package/pirates.md) ^3.0.2
- [resolve](https://npm.io/package/resolve.md) ^1.7.1
- [callsites](https://npm.io/package/callsites.md) ^2.0.0
- [is-var-name](https://npm.io/package/is-var-name.md) ^2.0.0

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [@crvouga/mockingbird-service-paddle](https://npm.io/package/@crvouga/mockingbird-service-paddle.md) — 0 weekly downloads
- [odata-prova](https://npm.io/package/odata-prova.md) — 0 weekly downloads
- [@jota-one/drosse](https://npm.io/package/@jota-one/drosse.md) — 0 weekly downloads

## Recent versions

- 2.0.1 (latest) — 2018-04-25
- 2.0.0 — 2018-04-25
- 1.0.0 — 2018-04-18

## README

# require-with-global

inject a global variable into a module require() tree

[![npm][npm-image]][npm-url]
[![travis][travis-image]][travis-url]
[![standard][standard-image]][standard-url]

[npm-image]: https://img.shields.io/npm/v/require-with-global.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/require-with-global
[travis-image]: https://img.shields.io/travis/goto-bus-stop/require-with-global.svg?style=flat-square
[travis-url]: https://travis-ci.org/goto-bus-stop/require-with-global
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: http://npm.im/standard

## Install

```
npm install require-with-global
```

## Usage

```js
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](LICENSE.md)

---
_Source: https://npm.io/package/require-with-global · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
