Swizzle a little something into your Node.js modules.
Requizzle provides a drop-in replacement for Node.js's
require() function. This replacement
allows you to change a module's source code at runtime.
You can use Requizzle in your test cases, or in production code if you like to live dangerously. Requizzle has been tested with Node.js 0.10 and 0.11.
Here's what Requizzle can do:
Look for modules in new places
With Requizzle, you can add directories to the module lookup path, which forces Node.js to search those directories for modules. This can be useful if:
- You're tired of writing code like
- You want to expose your app's modules to external plugins. (Without Requizzle, it can be tough to do this if the plugin is located outside of your app directory.)
Add code before or after the module's source code
Tamper with modules to your heart's delight by adding arbitrary code before or after the module's own source code.
Mess with child modules
When you use Requizzle to require a module, you can force each child module's
require method to
inherit your changes to the parent module. (By default, only the parent module is changed.)
Preserve strict-mode declarations
If a module starts with a strict-mode declaration, Requizzle keeps it in place. Your changes will appear after the strict-mode declaration.
Leave native modules alone
If you use one of Node.js's built-in modules, such as
path, Requizzle won't mess with it.
npm install requizzle
git clone git://github.com/hegemonic/requizzle.git cd requizzle npm install
The Requizzle module exports a single function, which returns a drop-in replacement for
When you call the function, you must pass in an
options object, which can include any of these
extras: A pair of functions that return text to insert before or after the module's source code. Each function accepts two parameters:
targetPath, the path to the required module, and
Moduleobject for the module's parent. Each function must return a string.
extras.before: A function that returns text to insert before the module's source code.
extras.after: A function that returns text to insert after the module's source code.
infect: Determines whether child modules are infected with the same changes as the parent module. Set to
trueto force child modules to inherit your changes. Defaults to
requirePaths: An array of additional paths to search for required modules. For example, if
requirePathsis set to
/usr/lib/junk/modules/mymodule.js, you can require the module as
mymodule. By default, the require path is not changed.
var requizzle = ;// Say hello and goodbye to each module.var logRequire =;// Prints "Hello /path/to/mymodule.js!" and "Goodbye /path/to/mymodule.js!"var myModule = ;// Look for modules in the current module's `lib` directory, and force child// modules to do the same.var path = ;var extraPathRequire =;// If `foo` needs to require a module in `./lib`, it can use `require('bar')`// instead of `require('./lib/bar')`.var foo = ;
Here are some problems you may run into when you use Requizzle, along with solutions to each problem. If you run into any problems that aren't addressed here, please file a new issue!
Requizzle slowed down my code! A lot!
Requizzle adds minimal overhead to the module-loading process. In some cases, it may even be faster
than Node.js's built-in
However, your code will run much slower than usual if you do both of the following:
- Use Requizzle's
- Require modules that have a lot of
require()calls within the scope of individual functions.
To fix this issue, find the module calls that are within function scope, and move them to each
module's top-level scope. You can find the biggest offenders by using Node.js's built-in
option to profile your app, then running node-tick to
create a report that shows the number of ticks per function.
Requizzle made my module do something weird!
Do you have any circular dependencies in the modules that aren't working? Circular dependencies can cause unusual behavior with Requizzle, just as they can without Requizzle. Try breaking the circular dependency.
Requizzle violates the sacred Law of Demeter! It's an unnatural abomination!
Requizzle is very loosely adapted from Johannes Ewald's rewire module, which is designed to modify a module's behavior for unit testing. If Requizzle doesn't meet your needs, please take a look at rewire!