I would like to have the ability to load a module that has been changed. I would have to of course unload the module first. Since this is a webserver setup, I am asking if there is a way to load the module in an async fashion, to avoid freezing the webserver for the duration of the read of the updated file.
Awhile back Node.JS removed the require.async function. So, on the latest version of Node.JS, what would be the recommended alternative?
- Should I read the entire file first, and then use the
Modulelibrary to parse the file contents. (as opposed to the default functionality of taking a file name to read and parse internally) How? - Should I outsource this job to some open-source library? Which one?
- Should I write my own module handler – my own implementation of
requireAsync? (I know how.)
Note: I do not want to do anything more than load a module async, so please do not recommend that I replace my setup with a new webserver routing framework.
I posted this answer, but you are welcome to post an improvement.
See the Node.JS source code.
Module.prototype.require = function(path) { return Module._load(path, this); };Abridged version of
_loadMy version of
require.async.jswill be something likeCaveats
statto be async. The actual reading of the file is regular async, so that is good._compile.