node.js require() cache - possible to invalidate?

后端 未结 17 1641
我寻月下人不归
我寻月下人不归 2020-11-22 06:52

From the node.js documentation:

Modules are cached after the first time they are loaded. This means (among other things) that every call to require(\'

相关标签:
17条回答
  • 2020-11-22 07:44

    The solutions is to use:

    delete require.cache[require.resolve(<path of your script>)]
    

    Find here some basic explanations for those who, like me, are a bit new in this:

    Suppose you have a dummy example.js file in the root of your directory:

    exports.message = "hi";
    exports.say = function () {
      console.log(message);
    }
    

    Then you require() like this:

    $ node
    > require('./example.js')
    { message: 'hi', say: [Function] }
    

    If you then add a line like this to example.js:

    exports.message = "hi";
    exports.say = function () {
      console.log(message);
    }
    
    exports.farewell = "bye!";      // this line is added later on
    

    And continue in the console, the module is not updated:

    > require('./example.js')
    { message: 'hi', say: [Function] }
    

    That's when you can use delete require.cache[require.resolve()] indicated in luff's answer:

    > delete require.cache[require.resolve('./example.js')]
    true
    > require('./example.js')
    { message: 'hi', say: [Function], farewell: 'bye!' }
    

    So the cache is cleaned and the require() captures the content of the file again, loading all the current values.

    0 讨论(0)
  • 2020-11-22 07:44

    rewire is great for this use case, you get a new instance with each call. Easy dependency injection for node.js unit testing.

    rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. You may

    inject mocks for other modules or globals like process leak private variables override variables within the module. rewire does not load the file and eval the contents to emulate node's require mechanism. In fact it uses node's own require to load the module. Thus your module behaves exactly the same in your test environment as under regular circumstances (except your modifications).

    Good news to all caffeine-addicts: rewire works also with Coffee-Script. Note that in this case CoffeeScript needs to be listed in your devDependencies.

    0 讨论(0)
  • 2020-11-22 07:45

    If you want a module to simply never be cached (sometimes useful for development, but remember to remove it when done!) you can just put delete require.cache[module.id]; inside the module.

    0 讨论(0)
  • 2020-11-22 07:49

    If you always want to reload your module, you could add this function:

    function requireUncached(module) {
        delete require.cache[require.resolve(module)];
        return require(module);
    }
    

    and then use requireUncached('./myModule') instead of require.

    0 讨论(0)
  • 2020-11-22 07:49

    I'd add to luff's answer one more line and change the parameter name:

    function requireCached(_module){
        var l = module.children.length;
        for (var i = 0; i < l; i++)
        {
            if (module.children[i].id === require.resolve(_module))
            {
                module.children.splice(i, 1);
                break;
            }
        }
        delete require.cache[require.resolve(_module)];
        return require(_module)
    }
    
    0 讨论(0)
提交回复
热议问题