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

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

提交回复
热议问题