node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »



Build Status

Generates require functions that act as if they were operating in a given path.


npm install require-like


A require function that acts as if it was executed in './lib/bar':

var requireLike = require('require-like');
var myRequire = requireLike(__dirname + '/lib/bar.js');
var myFoo = myRequire('./foo.js');
var foo = require('./lib/foo');
require('assert').strictEqual(myFoo, foo);


requireLike(path, [uncached])

Returns a require function that acts as if it was operating in the given path.

Setting the uncached parameter to true returns a function that by-passes the module cache.

Implementation Details

This module works by accessing some private node APIs. You shouldn't worry about that so, since I will make sure this module does not break in the future by either patching it, or making a patch for node that makes the needed APIs public.

What to do with this

I use this library for dependency injection in unit tests. However, you could also use it to create experimental require addons yourself.


require-like is licensed under the MIT license.