Gathering detailed insights and metrics for stealthy-require
Gathering detailed insights and metrics for stealthy-require
Gathering detailed insights and metrics for stealthy-require
Gathering detailed insights and metrics for stealthy-require
stealthy-require-no-leak
Drop-in replacement for stealthy-require that fixes a memory leak when being called multiple times.
continuous-stealthy-require
Requires a fresh, uncached module without causing a memory leak
jest-transform-stealthy-require
Transforms stealthy require calls into jest.isolateModules()
The closest you can get to require something with bypassing the require cache
npm install stealthy-require
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
ISC License
22 Stars
37 Commits
3 Forks
1 Watchers
1 Branches
2 Contributors
Updated on Dec 16, 2023
Latest Version
1.1.1
Package Id
stealthy-require@1.1.1
Size
4.65 kB
NPM Version
2.15.11
Node Version
4.8.0
Published on
May 09, 2017
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
This is probably the closest you can currently get to require something in node.js with completely bypassing the require cache.
stealthy-require
works like this:
The restrictions are:
This means you should have a close look at all internal require calls before you decide to use this library.
This is a module for node.js and is installed via npm:
1npm install stealthy-require --save
Let's say you want to bypass the require cache for this require call:
1var request = require('request');
With stealthy-require
you can do that like this:
1var stealthyRequire = require('stealthy-require'); 2 3var requestFresh = stealthyRequire(require.cache, function () { 4 return require('request'); 5});
The require cache is bypassed for the module you require (i.e. request
) as well as all modules the module requires (i.e. http
and many more).
Sometimes the require cache shall not be bypassed for specific modules. E.g. request
is required but tough-cookie
– on which request
depends on – shall be required using the regular cache. For that you can pass two extra arguments to stealthyRequire(...)
:
module
variable1var stealthyRequire = require('stealthy-require'); 2 3var requestFresh = stealthyRequire(require.cache, function () { 4 return require('request'); 5}, 6function () { 7 require('tough-cookie'); // No return needed 8 // You can require multiple modules here 9}, module);
require.cache
. However, as of browserify@13.0.1
the cache is passed as the 6th argument to CommonJS modules. Thus you can pass this argument instead:1// Tweak for Browserify - using arguments[5] instead of require.cache 2var requestFresh = stealthyRequire(arguments[5], function () { 3 return require('request'); 4});
If you are using stealthy-require
in node.js and repeatedly require fresh module instances the module.children
array will hold all module instances which prevents unneeded instances to be garbage collected.
Assume your code calls doSomething()
repeatedly.
1var stealthyRequire = require('stealthy-require'); 2 3function doSomething() { 4 5 var freshInstance = stealthyRequire(require.cache, function () { 6 return require('some-module'); 7 }); 8 9 return freshInstance.calc(); 10 11}
After doSomething()
returns freshInstance
is not used anymore but won’t be garbage collected because module.children
still holds a reference. The solution is to truncate module.children
accordingly:
1var stealthyRequire = require('stealthy-require'); 2 3function doSomething() { 4 5 var initialChildren = module.children.slice(); // Creates a shallow copy of the array 6 7 var freshInstance = stealthyRequire(require.cache, function () { 8 return require('some-module'); 9 }); 10 11 module.children = initialChildren; 12 13 return freshInstance.calc(); 14 15}
The slice
operation removes all new module.children
entries created during the stealthyRequire(...)
call and thus freshInstance
gets garbage collected after doSomething()
returns.
1// 1. Load stealthy-require 2var stealthyRequire = require('stealthy-require'); 3// This does nothing but loading the code. 4// It has no side-effects like patching the module loader or anything. 5 6// Any regular require works as always. 7var request1 = require('request'); 8 9// 2. Call stealthyRequire with passing the require cache and a callback. 10var requestFresh = stealthyRequire(require.cache, function () { 11 12 // 2a. Before this callback gets called the require cache is cleared. 13 14 // 2b. Any require taking place here takes place on a clean require cache. 15 // Since the require call is part of the user's code it also works with module bundlers. 16 return require('request'); 17 // Anything returned here will be returned by stealthyRequire(...). 18 19 // 2c. After this callback gets called the require cache is 20 // - cleared again and 21 // - restored to its old state before step 2. 22 23}); 24 25// Any regular require again works as always. 26// In this case require returns the cached request module instance. 27var request2 = require('request'); 28 29// And voilà: 30request1 === request2 // -> true 31request1 === requestFresh // -> false
To set up your development environment for stealthy-require
:
cd
to the main folder,npm install
,npm install gulp -g
if you haven't installed gulp globally yet, andgulp dev
. (Or run node ./node_modules/.bin/gulp dev
if you don't want to install gulp globally.)gulp dev
watches all source files and if you save some changes it will lint the code and execute all tests. The test coverage report can be viewed from ./coverage/lcov-report/index.html
.
If you want to debug a test you should use gulp test-without-coverage
to run all tests without obscuring the code by the test coverage instrumentation.
undefined
entries from appearing in require.cache
(Thanks to @jasperjn from reporting this in issue #4)In case you never heard about the ISC license it is functionally equivalent to the MIT license.
See the LICENSE file for details.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/27 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More