NoCompiling Node.JS modules separately and load runtime
Problem
Can we compile Node.js modules separately and load them runtime as we can do with modules in Flex? My requirement is to avoid restarting Node.js every time we change the code of modules.
Solution
I don't think you can do hot swapping while running a node.js application.
Node.js keeps the require
d module in cache during the lifetime of the application. However, you can do something like
delete require.cache[require.resolve('./module.js')]
Or if you want to reload all the modules, then
for (var key in require.cache) {
delete require.cache[key];
}
This will delete the cached key from require
cache and force node.js to reload the module next time when require('./module.js')
occurs.
Discussion
View additional discussion.