Require multiple times

1920 views javascript
1

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

answered question

why do you want to import twice? you can my_script to print twice

Is this node, webpack, other?

@andrewgi I don't want to edit my_script. Humour me for this one

@AndyRay Nodejs, check the question tags

3 Answers

13

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.

posted this
8

require caches the result of require statements, which is why you are only seeing IMPORTED once. It's executes my_script once and will return whatever you exported.

You can invalidate the cache with something like -- this might be okay for testing, but not a great general solution. And it's not clear why you need the immediately executed function anyway:

require("./my_script")
delete require.cache[require.resolve('./my_script')]
require("./my_script");

But you could also change the script to export a function which you then call:

//my_script.js
module.exports = function() {
    console.log("IMPORTED");
}

Then call the function twice:

require("./my_script")();
require("./my_script")();

posted this
9

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

posted this

Have an answer?

JD

Please login first before posting an answer.