githubEdit

Stealthy-Require

Build Statusarrow-up-right Coverage Statusarrow-up-right Dependency Statusarrow-up-right

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:

  1. It clears the require cache.

  2. It calls a callback in which you require your module(s) without the cache kicking in.

  3. It clears the cache again and restores its old state.

The restrictions are:

  • Native modules cannot be required twice.arrow-up-right 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.

Installation

NPM Statsarrow-up-right

This is a module for node.js and is installed via npm:

npm install stealthy-require --save

Usage

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

Usage with Module Bundlers

Preventing a Memory Leak When Repeatedly Requiring Fresh Module Instances in Node.js

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.

Technical Walkthrough

Contributing

To set up your development environment for stealthy-require:

  1. Clone this repo to your desktop,

  2. in the shell cd to the main folder,

  3. hit npm install,

  4. hit npm install gulp -g if you haven't installed gulp globally yet, and

  5. 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.

Change History

License (ISC)

In case you never heard about the ISC licensearrow-up-right it is functionally equivalent to the MIT license.

See the LICENSE filearrow-up-right for details.

Last updated

Was this helpful?