Question

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.

Was it helpful?

Solution

I don't think you can do hot swapping while running a node.js application.

Node.js keeps the required 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top