How to remove module after “require” in node.js?

后端 未结 5 1969
感动是毒
感动是毒 2020-11-29 00:27

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 a

5条回答
  •  半阙折子戏
    2020-11-29 00:46

    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.

提交回复
热议问题