Is it possible to hot reload external js files in node.js based on its timestamp?
I know node.js caches module after first time load from here: http://nodejs.org/docs/latest/api/modules.html#modules_caching
Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.
And I also know if I need to reload it I can do like this:
// first time load
var foo = require('./foo');
foo.bar()
...
// in case need to reload
delete require.cache[require;.resolve('./foo')]
foo = require('./foo')
foo.bar();
But I wonder if there's any native support in node.js that it watches the file and reload it if there's any changes. Or I need to do it by myself?
pseudo code like
// during reload
if (timestamp for last loaded < file modified time)
reload the file as above and return
else
return cached required file
P.S. I am aware of supervisor and nodemon and don't want to restart server for reloading some specific modules.