node package manager
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »



Deprecated in favour of require-uncached.

For deprecation reason, see

tl;dr; avoiding duplication of effort in the open-source world.


Travis build status NPM version

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.


require-new requires a new module object.

require-new does not affect the state or behavior of require method.

require-new has been designed to be used for module testing.


Load require-new module and use it to load a module just as you would with require:

var requireNew = require('require-new'),
    myModule = requireNew('my-module');


If you have a module rand.js:

module.exports = Math.random();

Then requiring this module several times will result in the same response:

require('./rand.js'); // 0.697190385311842 
require('./rand.js'); // 0.697190385311842 

Modules are cached in a require.cache object when they are required.

require-new deletes the key value from the require.cache object associated with the module you are requesting, making the module reload:

requireNew('./rand.js'); // 0.2123227424453944 
requireNew('./rand.js'); // 0.5403654584661126 

It then restores the state of the require.cache object to ensure that it does not affect the behavior of require:

require('./rand.js'); // 0.48205413995310664 
requireNew('./rand.js'); // 0.12475096038542688 
requireNew('./rand.js'); // 0.2615479789674282 
require('./rand.js'); // 0.48205413995310664 

Alternative Design

You can export a function and call that function. This will make the module execute code multiple times.

module.exports = function () {
    return Math.random();
require('rand.js')(); // 0.561616780469194 
require('rand.js')(); // 0.6468832329846919 

See discussion on Stack Overflow to consider the pros and cons.


Download using NPM:

npm install require-new