Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

batchdelcache

Package Overview
Dependencies
Maintainers
2
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

batchdelcache

batch delete module cache in node safely

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29
increased by107.14%
Maintainers
2
Weekly downloads
 
Created
Source

batchdelcache

Delete module cache safely. This project is inspired by decache.

Build Status Coverage Status

Installation

# use npm
npm install batchdelcache
# or use yarn
yarn global add batchdelcache

Environment

Node.js 10+

Background

Deleting module cache is the precondition of 'hot reloading'. Typically, we delete the module cache by:

delete require.cache[require.resolve('xxx')]

However, this method can cause memory leakage. It clears the module cache in Module._cache which is referenced by require.cache, but it doesn't eliminate the reference in the array of xxx.parent.children.

batchdelcache provides a way to eliminate both kinds of reference:

batchdelcache('xxx')

In most cases, this is enough to clean up all references, with the following exception:

{
    id: 'parentModule',
    children: [{
        id: 'childModuleA',
        children: [{
            id: 'targetModule'
        }]
    }, {
        id: 'childModuleB',
        children: [{
            id: 'targetModule'
        }]
    }]
}

You will find targetModule still in the children of childModuleA after calling batchdelcache(targetModule) in childModuleB. In this condition, you can pass true as the second argument to batchdelcache: batchdelcache(targetModule, true). batchdelcache will traverse the whole module tree to delete targetModule from children of every module.

When passing rootPath as the third argument, batchdelcache will take it as the starting point. It is a recommended way to save time traversing the module tree.

Usage

Delete cache of target module and clear reference in parent's children:

const batchdelcache = require('batchdelcache')

let five = require('./five')

five.num = 6

batchdelcache(['./five'])

five = require('./five')

console.log(five.num) // 5

Pass true as the second argument to delete cache of target module from the whole module tree.

const batchdelcache = require('batchdelcache')

let five = require('./five')
let six = require('./six')

five.num = 6
six.sum = 7

batchdelcache(['./five', './six'], true)

five = require('./five')
six = require('./six')

console.log(five.num) // 5
console.log(six.num) // 6

When the second argument is true, you can specify the rootPath from which clear the reference of the target module.

This is a recommended way to make the delete operation faster:

const batchdelcache = require('batchdelcache')

let five = require('./five')

five.num = 6

batchdelcache('./five', true, '../../root')

five = require('./five')

console.log(five.num) // 5

Notice

Don't uese delete require.cache[xxx] with batchdelcache, cause batchdelcache will check if a module is referenced by Module._cache.

//parent.js
require('./a')
require('./b')
//a.js
require('./mod')
delete require.cache[require.resolve('./mod')]
//b.js
const batchdelcache = require('batchdelcahce')
batchdelcache('./mod', true) // delcache can not eliminate the reference of mod in children of a.js

FAQs

Package last updated on 23 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc