Reputation: 2655
File my_script.js
:
(function() {
console.log("IMPORTED");
})();
Calling this file (run_me.js
) should cause IMPORTED
to print twice:
require("./my_script");
require("./my_script");
However it only prints once.
How can I change run_me.js
so that IMPORTED
is printed to the console twice.
Assume for this question, no changes can be made to my_script.js
Upvotes: 21
Views: 12243
Reputation: 707158
require()
caches its results. So, the first time a module is required, then its initialization code runs. After that, the cache just returns the value of module.exports without running the initialization code again. This is a very desirable feature of node.js modules.
If you want code to be run each time, then you should export a function that you can call after you require it like this:
Your module:
module.exports = function() {
console.log("IMPORTED");
}
Requiring it and running the code each time
require("./my_script")();
require("./my_script")();
Also, please note that there is no reason to use an IIFE in a module. The node.js module is automatically wrapped in a private function already so you don't need to do it again.
As you now say in a comment (but your question does not directly say), if you don't want to edit my_script at all (which is simply the wrong way to solve this issue), then you have to delete the module from the node.js cache before requiring it again which can be done like this:
delete require.cache[require.resolve('./my_script')];
I would not recommend this as a solution. It's not the proper way to code in node.js. It's a hack work-around. And, it is not compatible with ESM modules.
Upvotes: 43
Reputation: 2094
If you use jest and want code to be run each time for testing, you can use jest.isolateModules:
jest.isolateModules(() => {
require("./my_script");
});
jest.isolateModules(() => {
require("./my_script");
});
Upvotes: 10
Reputation: 510
I don't think it is possible without modifying the myscript.js
file. Especially since as you show it, it doesn't export anything.
It will execute the first time you require it (which is why you see "Imported" once), but then nothing will happen on future calls to require because the "cached" value (ie. module.exports
) which is returned is empty.
See below for an example of what I think you want (except that myscript.js has been modified). The biggest difference is that in your original myscript.js
file the function was actually executed, while in the example below it is just defined, and then actually executed in the require
call in the run_me.js
file.
File myscript.js
:
module.exports = () => console.log("Imported");
File run_me.js
:
require('myscript.js')(); // note the () at the end which actually calls the function
require('myscript.js')(); // note the () at the end which actually calls the function
Upvotes: 0
Reputation: 64
You can use this package, it is an npm module that will clear the cache and load a module from source fresh each time.
https://www.npmjs.com/package/require-uncached
const requireUncached = require('require-uncached');
require('./foo')();
//=> 1
require('./foo')();
//=> 2
requireUncached('./foo')();
//=> 1
requireUncached('./foo')();
//=> 1
Upvotes: -1