Skip to content

Instantly share code, notes, and snippets.

@magicdawn
Created October 12, 2014 01:14
Show Gist options
  • Save magicdawn/5d6f53e1d3020cec112f to your computer and use it in GitHub Desktop.
Save magicdawn/5d6f53e1d3020cec112f to your computer and use it in GitHub Desktop.
nodejs module 热更新
/*
require -> Module.prototype.require -> Module.load(request,module)
require.reslove -> Module._resolveFilename(request, module);
*/
var Module = module.constructor
Module.prototype._require = function(request) {
var res = this.require(request)
// delete cache
var filename = Module._resolveFilename(request, this);
delete Module._cache[filename]
return res;
};
require('./_require')
var timer = setInterval(function() {
console.log(module._require("./test"));
},1000)
setTimeout(function() {
timer.unref()
}, 1000 * 30);
// 修改module.exports 看输出
module.exports = 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment