Skip to content
watchFile and reload modules dynamically for node.js (useful for front end development) - it would be nice if someone could implement the new require.cache support with my require as falback
Find file
Failed to load latest commit information.
example.js modified: example.js Oct 30, 2010
index.js modifid readme and examples Oct 30, 2010
mymodule_example.js update example Oct 29, 2010

node.js hot reload of js files. (it is not restart, more like erlang)

hotreload.path, reload_callback)

watch script file, onchange load the script file and call callback

script is string

reload_callback(newmodule) is a function, it has an argument - the loaded module object., script , reload_callback)

watch one file and load another.

watchpath can be a string or an array script is string

hotreload.watchrel(script, reload_callback)

watch script file relative to @hotreload.path@

hotreload.watchrel(watchpath, script , reload_callback)



var http = require('http');
var hotreload= require('node-hot-reload');

//1st require the module normaly

var mymodule= require('./mymodule_example'); 

//2nd watch it and reload it dinamicaly

// explained example:

hotreload.watchrel('mymodule_example.js', function (newmodule){

   /* you can put here staff to make your module
      look like it was initialized well. */; // take care to copy previous data

   //mymodule.init(); // init the module before if possible,
                      // it will save error time.
   // option 1
   // replace reference - probably the best way
   // but you must update all references you have 
   // replace reference - probably the best way
   // but you must update all references you have 


   // option 2
   // copy properties from new module to old module
   // as a solution to not having to update
   // references to module object

   // option 3
   //   manually patch the old object with parts of the new object.

   // initialization or more initialization after switching to the new module
   // mymodule.init();
   // better don't do it, you may get errors,
   // because of not well-initialized module.


// simple example1:
//  function (newmodule){ mymodule=newmodule; } );

// simple example2:
//  function (newmodule){ hotreload.copy(mymodule,newmodule); } );

// idea: add loader function to automate reloading of modules:

http.createServer(function (request, response) {

   response.writeHead(200, {'Content-Type': 'text/plain'});
   response.end(mymodule.time() + '\n');

  //to do, main module reloadable instead of above you might use: 


console.log('Server running at');


console.log('mymodule loaded');'test'; Date).toString();
exports.time=function ()
                      //  edit the number to see it working
 return  " test number 17 name: "" , loaded at: ";
Something went wrong with that request. Please try again.