Stealthy-Require
Last updated
Last updated
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:
It clears the require cache.
It calls a callback in which you require your module(s) without the cache kicking in.
It clears the cache again and restores its old state.
The restrictions are:
Native modules cannot be required twice. Thus this module bypasses the require cache only for non-native (e.g. JS) modules.
The require cache is only bypassed for all operations that happen synchronously when a module is required. If a module lazy loads another module at a later time that require call will not bypass the cache anymore.
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:
Let's say you want to bypass the require cache for this require call:
With stealthy-require
you can do that like this:
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(...)
:
A callback that requires the modules that shall be required without bypassing the cache
The module
variable
Webpack works out-of-the-box like described in the Usage section above.
Browserify does not expose 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:
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.
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:
The slice
operation removes all new module.children
entries created during the stealthyRequire(...)
call and thus freshInstance
gets garbage collected after doSomething()
returns.
To set up your development environment for stealthy-require
:
Clone this repo to your desktop,
in the shell cd
to the main folder,
hit npm install
,
hit npm install gulp -g
if you haven't installed gulp globally yet, and
run gulp 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.
v1.1.1 (2017-05-08)
Fix that stops undefined
entries from appearing in require.cache
(Thanks to @jasperjn from reporting this in issue #4)
v1.1.0 (2017-04-25)
Added ability to disable bypassing the cache for certain modules (Thanks to @novemberborn for suggesting this in issue #3)
Added section in README about a potential memory leak (Thanks to @Flarna and @novemberborn for bringing that up in issue #2)
Performance optimizations (Thanks to @jcready for pull request #1)
v1.0.0 (2016-07-18)
Breaking Change: API completely changed. Please read the Usage section again.
Redesigned library to support module bundlers like Webpack and Browserify
v0.1.0 (2016-05-26)
Initial version
In case you never heard about the ISC license it is functionally equivalent to the MIT license.
See the LICENSE file for details.