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

后端 未结 17 1655
我寻月下人不归
我寻月下人不归 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:32

    There's a Simple Module for that (with tests)

    We had this exact issue while testing our code (delete cached modules so they can be re-required in a fresh state) so we reviewed all the suggestions of people on the various StackOverflow Questions & Answers and put together a simple node.js module (with tests):

    https://www.npmjs.com/package/decache

    As you would expect, works for both published npm packages and locally defined modules. Windows, Mac, Linux, etc.

    How? (usage)

    Usage is pretty simple:

    install

    Install the module from npm:

    npm install decache --save-dev

    Use it in your code:

    // require the decache module:
    const decache = require('decache');
    
    // require a module that you wrote"
    let mymod = require('./mymodule.js');
    
    // use your module the way you need to:
    console.log(mymod.count()); // 0   (the initial state for our counter is zero)
    console.log(mymod.incrementRunCount()); // 1
    
    // delete the cached module:
    decache('./mymodule.js');
    
    //
    mymod = require('./mymodule.js'); // fresh start
    console.log(mymod.count()); // 0   (back to initial state ... zero)
    

    If you have any questions or need more examples, please create a GitHub issue: https://github.com/dwyl/decache/issues

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

    The documentation says:

    Modules are cached in this object when they are required. By deleting a key value from this object, the next require will reload the module. This does not apply to native addons, for which reloading will result in an error.

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

    Following two step procedure is working perfectly for me.

    After changing Model file i-e 'mymodule.js' dynamically, you need to Delete precompiled model in mongoose model first then reload it using require-reload

    Example:
            // Delete mongoose model
            delete mongoose.connection.models[thisObject.singular('mymodule')]
    
            // Reload model
            var reload = require('require-reload')(require);
            var entityModel = reload('./mymodule.js');
    
    0 讨论(0)
  • 2020-11-22 07:36

    I am not 100% certain of what you mean by 'invalidate', but you can add the following above the require statements to clear the cache:

    Object.keys(require.cache).forEach(function(key) { delete require.cache[key] })
    

    Taken from @Dancrumb's comment here

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

    If it's for unit tests, another good tool to use is proxyquire. Everytime you proxyquire the module, it will invalidate the module cache and cache a new one. It also allows you to modify the modules required by the file that you are testing.

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

    I made a small module to delete module from the cache after loading. This forces reevaluation of the module next time it is required. See https://github.com/bahmutov/require-and-forget

    // random.js
    module.exports = Math.random()
    const forget = require('require-and-forget')
    const r1 = forget('./random')
    const r2 = forget('./random')
    // r1 and r2 will be different
    // "random.js" will not be stored in the require.cache
    

    PS: you can also put "self-destruct" into the module itself. See https://github.com/bahmutov/unload-me

    PSS: more tricks with Node require in my https://glebbahmutov.com/blog/hacking-node-require/

    0 讨论(0)
提交回复
热议问题