Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove module after "require" in node.js?

Let say, after I require a module and do something as below:

var b = require('./b.js'); --- do something with b --- 

Then I want to take away module b (i.e. clean up the cache). how I can do it?

The reason is that I want to dynamically load/ remove or update the module without restarting node server. any idea?

------- more -------- based on the suggestion to delete require.cache, it still doesn't work...

what I did are few things: 1) delete require.cache[require.resolve('./b.js')]; 2) loop for every require.cache's children and remove any child who is b.js 3) delete b 

However, when i call b, it is still there! it is still accessible. unless I do that:

b = {}; 

not sure if it is a good way to handle that. because if later, I require ('./b.js') again while b.js has been modified. Will it require the old cached b.js (which I tried to delete), or the new one?

----------- More finding --------------

ok. i do more testing and playing around with the code.. here is what I found:

1) delete require.cache[]  is essential.  Only if it is deleted,   then the next time I load a new b.js will take effect. 2) looping through require.cache[] and delete any entry in the   children with the full filename of b.js doesn't take any effect.  i.e. u can delete or leave it.  However, I'm unsure if there is any side effect.  I think it is a good idea to keep it clean and delete it if there is no performance impact. 3) of course, assign b={} doesn't really necessary, but i think it is   useful to also keep it clean. 
like image 420
murvinlai Avatar asked Mar 27 '13 18:03

murvinlai


People also ask

Does Nodejs cache require?

Per the node documentation, modules are cached after the first time they are loaded (loaded is synonymous with 'required'). They are placed in the require. cache . This means that every future require for a previously loaded module throughout a program will load the same object that was loaded by the first require.

Can I use both import and require in node JS?

If nodejs thinks you have a CJS file, you cannot use import (that is only for ESM modules). You can also modify the package. json file that controls your particular script (by adding "type": "module" and force your file to an ESM module if you want (which allows you to use . js files as ESM modules).

How do I require a Node module?

You can think of the require module as the command and the module module as the organizer of all required modules. Requiring a module in Node isn't that complicated of a concept. const config = require('/path/to/file'); The main object exported by the require module is a function (as used in the above example).

What is the use of require in node JS?

require() is used to consume modules. It allows you to include modules in your app. You can add built-in core Node. js modules, community-based modules (node_modules), and local modules too.


1 Answers

You can use this to delete its entry in the cache:

delete require.cache[require.resolve('./b.js')] 

require.resolve() will figure out the full path of ./b.js, which is used as a cache key.

like image 154
robertklep Avatar answered Sep 20 '22 17:09

robertklep