Hotload - Hot code reload for NodeJS
Version 0.0.5
TL;DR:
// lib.jsmoduleexports = "key": "value" // index.jshotload = ; //lib = require("lib");lib = ; ;
Install
> npm install hotload
Usage
Introduction
The purpose is reload library without application shut down.
It is probably not suitable for production use.
Hotload only works on object modules. Non-object modules (E.g. module.exports = "abc"
) will be require
d normally but they cannot be hot reloaded. If you don't see why you should google about JavaScript's variable references and its pass-by-value nature.
// index.jshotload = ; // First argument is the same as for `require`. Second argument (callback) is optional.// Callback's first argument is the module object, which is exactly the same object// as returned by `hotload`.lib = ; // Callback function is called on first module load// and on every module reload.
From now on if lib.js
is modified it will be reloaded. How does it work? On lib.js
file change hotload replaces all properties of original lib
object with new ones.
E.g. old lib.js
could be:
// old lib.jsmoduleexports = "a": 10 "b": 11;
And new lib.js
could be
// new lib.jsmoduleexports = "b": 21 "c": 22;
In that case during runtime of index.js
when we save lib.js
(old version to new version) then lib
object will become:
"b": 21 "c": 22
So after hot reload lib
object can still be used. You don't need to use callback method to replace your references at all, it just works.
Additional information
Hotload will look out for imported object's methods hlInit
and hlUnload
. E.g.:
moduleexports = { console; } { console; }
As mentioned in example's hlUnload
function -- if your module has event listeners or any other long running tasks (e.g. setInterval
) you should shut them down while unloading module, because if you don't when module is reloaded it will duplicate event listeners.
You could say "Hey, wait a second! After module reload old module is gone, and I don't have access to event listeners so they must be gone too!". Wrong. They are still running and worse -- you don't have access to them any more!