Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hot swapping in Node.JS. Possible?

Is it possible to implement or at least simulate code hot swapping in Node.JS? If yes, how?

Hot swapping (frequently called hot plugging) is replacing or adding components without stopping or shutting down the system. Erlang and Lisp support hot swapping natively.

like image 917
adelriosantiago Avatar asked Dec 30 '17 23:12

adelriosantiago


People also ask

How do I reload a module in node hotswap?

Events Local variables Configuration Todo Just write this in your main file (or in the repl): And then add this line to modules you want to be reloaded (it tells node-hotswap to watch this file): Now if you change your modules, they will be reloaded automatically.

What is hot-swap?

Hot-Swap arrangements are sold for LAN (Local Area Network ), SCSI (Small Computer System Interface), an IDE ( Integrated Drive Electronics ). Hot swapping works by providing a rack for the device that gives the appearance of a seamless connection to the computer’s bus or controller.

Is it normal for nodemon to reload the whole HTTP Server?

Recently I was working on a NextJs project with a custom Node server. I was running the server with Nodemon, which is pretty normal, but every time I change a server file, the whole http server has to reload. And even worse as a Next server it has to spin up the Next app as well.

What is the use of node cache?

Well, a little known fact is that Node maintains a cache (or store/record) of modules. As your code encounters a require statement, Node will load the module and then store it in thee require.cache. Each subsequent load of that module will load it from the require.cache, instead of reading the file again (performance reasons yeah).


1 Answers

For commonjs modules (original node.js module system) you can hot-swap modules by deleting its cache and re-requiring them:

delete require.cache[require.resolve('my-module')];
require('my-module');

I'm not sure if this works with es6 modules.

Of course, this needs to be done everywhere the module is loaded because otherwise your code will be using the objects and functions returned by the previous version of the module that is still in RAM.

One way to trigger a cascading reload is to make your main code also a module that is executed by a simple script that requires it. Then reloading your main module would cause it to reload other modules that it uses.

You may not need hot-swapping

In actual practice however you may find that you don't really need hot-swapping. Most node.js servers take milliseconds to boot. Part of the reason for this is most I/O libraries in node.js connect to external services lazily. Node.js servers generally don't wait for database connection to succeed before executing the rest of the code. Instead it will try to connect to the database the first time you make a database request.

Also, javascript is a fast language to parse and compile (by necessity because you send the source to web pages)

like image 157
slebetman Avatar answered Oct 02 '22 09:10

slebetman