# hotswap-module

> Replace a Node.js module with another module without any code changes

Latest version **1.0.2** (published 2018-07-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install hotswap-module
pnpm add hotswap-module
yarn add hotswap-module
bun add hotswap-module
```

Provides the command `hotswap-module`.

## 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 | 1.0.2 |
| Published | 2018-07-09 |
| First published | 2018-07-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 4.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 15 |
| Author | Thomas Watson |
| Maintainers | watson |
| Keywords | debug, debugging, swap, replace, require, module, modules, test, testing, import |

## Links

- npm: https://www.npmjs.com/package/hotswap-module
- Repository: https://github.com/watson/hotswap-module
- Homepage: https://github.com/watson/hotswap-module#readme
- Issues: https://github.com/watson/hotswap-module/issues
- npm.io page: https://npm.io/package/hotswap-module

## Dependencies (2)

- [import-cwd](https://npm.io/package/import-cwd.md) ^2.1.0
- [require-in-the-middle](https://npm.io/package/require-in-the-middle.md) ^3.0.0

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2018-07-09
- 1.0.1 — 2018-07-09
- 1.0.0 — 2018-07-09

## README

# hotswap-module

Replace the return value of `require('some-module')` with the return
value of another module in any app without any code changes.

[![npm](https://img.shields.io/npm/v/hotswap-module.svg)](https://www.npmjs.com/package/hotswap-module)
[![build status](https://travis-ci.org/watson/hotswap-module.svg?branch=master)](https://travis-ci.org/watson/hotswap-module)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)

## Installation

```
npm install hotswap-module --global
```

## CLI Usage

The following three commands all run the `script.js` file and replace
any call to `require('stream')` with `require('readable-stream')`.

If `hotswap-module` isn't installed, simply use npx to run it:

```sh
npx hotswap-module stream readable-stream script.js
```

If `hotswap-module` is installed globally:

```sh
hotswap-module stream readable-stream script.js
```

If `hotswap-module` is installed as a dependency:

```sh
HOTSWAP=stream,readable-stream node -r hotswap-module script.js
```

If the script for which you wish to hotswap a module is being started
indirectly, e.g via `npm test`, you can use the `NODE_OPTIONS`
environment variable to require the `hotswap-module`:

```sh
HOTSWAP=stream,readable-stream NODE_OPTIONS="-r hotswap-module" npm test
```

## Programmatic Usage

```js
const hotswap = require('hotswap-module')

// Call hotswap() with the name of the module you wish to replace and
// the exports object of the replacement module
hotswap('stream', require('readable-stream'))
```

## CLI API

```sh
hotswap-module <old-name> <new-name> <script>
```

## Programmatic API

### `hotswap(name, exports)`

Arguments:

- `name` - The name of the module you whish to replace
- `exports` - The new value of the modules exports object

It's important that you call the `hotswap` function before the module
defined by `name` is required for the first time.

## License

[MIT](https://github.com/watson/hotswap-module/blob/master/LICENSE)

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