drex
drex
- "dynamic require() extension".
Dynamic version of Node's require() - loads fresh copy of the module every time the module file is changed.
Insanely simple, and/but astonishingly useful grab of bits, which has been brought to this world out of severe necessity.
Here is why:
- all re-loaders I was able to find reload the whole node process, which means that the context of the process is gone;
- but, sometimes, you don't want that! You want your process to continue running intact, at least for those clients who already deep in it, and would choke if process forgets about them;
- sometime you have a little (or BIG) piece of code which you constantly change and, for G-d sake, do not want to sacrifice the whole your Node process for, but
- you don't want to loose the benefits of CommonJS/require supported modularity of your code.
Here comes drex
, and it comes like this:
var drex = ; ... node code node code node code ... // here goes my frea[ky/quently updated] piece of code, which lives in a js file called mucode.js:drex;
Here is an example (and the reason I had to comeup with drex) from the real life when drex is irreplaceable:#
frequently updated/added socket.io event handlers:
iosockets
API#
drex.require(<module reference>, <callBack>);
where:
-
<module reference>
resolvable Node module reference, such as './module.js' -
<callBack>
function which is being called when the module has beenrequire()d
.callBack signature:
callBack(module, filepath)
where:
module
: reference to the require()d module, andfilepath
: full path to the module's file
Installation using NPM#
npm install drex
Even more simple installation#
Just copy drex.js
to the place where Node can find it, i.e. for require('./drex.js')
- to the root of your Node project.
tl;dr#
drex
is watching a module for updates and cleanly re-requires the module after the update.
New code is being require()
d as if the new code is a totally different module, so require.cache
is not a problem.
License#
GPL
Enjoy