# wipe-node-cache

> Wipes node.js cache in a controlled way.

Latest version **2.1.2** (published 2020-05-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install wipe-node-cache
pnpm add wipe-node-cache
yarn add wipe-node-cache
bun add wipe-node-cache
```

## 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.1.2 |
| Published | 2020-05-02 |
| First published | 2016-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 17 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Anton Korzunov |
| Maintainers | kashey |
| Keywords | require, cache, injection, test, mock, stub, clean cache |

## Links

- npm: https://www.npmjs.com/package/wipe-node-cache
- Repository: https://github.com/theKashey/wipeNodeCache
- Homepage: https://github.com/theKashey/wipeNodeCache#readme
- Issues: https://github.com/theKashey/wipeNodeCache/issues
- npm.io page: https://npm.io/package/wipe-node-cache

## 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
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 2.1.2 (latest) — 2020-05-02
- 2.1.1 — 2020-02-14
- 2.1.0 — 2018-11-09
- 2.0.0 — 2018-10-20
- 1.1.0 — 2017-12-07
- 1.0.2 — 2017-05-04
- 1.0.1 — 2016-12-29
- 1.0.0 — 2016-12-29

## README

**Keep your cache clear** – as my mom always says.

wipeNodeCache – cleans, clears and wipes all old dirty modules from node.js internal require.cache. 

Useful for testing purposes when you need to freshly require a module. Or two.
Or just keep all modules fresh, for example for proxyquire.

## Install

```sh
$ npm install --save wipe-node-cache
```

## Usage

```js
// foo.js
var i = 0;
module.exports = function () {
	return ++i;
};
```

```js
var wipe = require('wipe-node-cache');

require('./foo')();
//=> 1

require('./foo')();
//=> 2

wipe(null, function(){return true;})

require('./foo')();
//=> 1 . Module is fresh now
```

But this is simply, and stupid way. We can do it better!

## API

### wipe(object1, filterCallback, bubbleCallback)

Foreach module in system wipe will call _filterCallback_ with 2 arguments – object1 and moduleName(absolute))
And you shall return true, if you want to wipe this module.

After first pass wipe will enter bubbling stage, and will wipe all modules, which use first ones.
Each time _bubbleCallback_ will be called with 1 argument - moduleName.
And you can decide - purge it, or not. 

## Examples

(see examples in source)
```js
function resolver(stubs, fileName, module) {
  return !fileName.indexOf('node_modules') > 0
}

// wipe everything, except node_modules
wipe(null, resolver, function (moduleName) {
  return !resolver(null, moduleName);
});
```

```js
function resolver(stubs, fileName, module) {
  var dirname = module ? path.dirname(module) : '';
  var requireName = fileName;
  if (dirname) {
    requireName = fileName.charAt(0) == '.' ? path.normalize(dirname + '/' + fileName) : fileName;
  }

  for (var i in stubs) {
    if (requireName.indexOf(i) > 0) {
      return stubs[i];
    }
  }
}

// wipe anything from helpers, and app.js.
// but keep hands off node_modules and core during bubbling.
wipe({
  'helpers/*': true,
  'App.js': true
}, resolver, function (moduleName) {
  return !(moduleName.indexOf('node_modules') > 0) && !(moduleName.indexOf('core') > 0)
});
```

## Related

- [proxyquire](https://github.com/thlorenz/proxyquire) - Usefull testing tool, but with bad caching politics.

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