Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

NoCompiling Node.JS modules separately and load runtime

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.

Problem courtesy of: nish

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.

Solution courtesy of: Салман

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

NoCompiling Node.JS modules separately and load runtime

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×