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 ponents without stopping or shutting down the system. Erlang and Lisp support hot swapping natively.
Is it possible to implement or at least simulate code hot swapping in Node.JS? If yes, how?
Share Improve this question edited Dec 31, 2017 at 0:02 adelriosantiago asked Dec 30, 2017 at 23:56 adelriosantiagoadelriosantiago 8,14410 gold badges45 silver badges74 bronze badges 4Hot swapping (frequently called hot plugging) is replacing or adding ponents without stopping or shutting down the system. Erlang and Lisp support hot swapping natively.
- @baao I don't think that the question marked as duplicate is really helpful here. Hot swapping code in the browser is pletely different than doing it in Node. Looking for a Node.JS solution. – adelriosantiago Commented Dec 31, 2017 at 0:04
- Reopened it.... – baao Commented Dec 31, 2017 at 0:07
- 1 Perhaps to a very limited extent, but only with code cooperation for state variables in closures, modules, etc... It would not be possible in a general sense to just "reload" a node.js module and have all the code using that module still continue to work just fine, but automatically start using the new code. If you look at all the tools available for updating code in a running server such as nodemon or node-supervisor, they all do a server restart. – jfriend00 Commented Dec 31, 2017 at 0:50
- You can look at nodules or live-node. Not sure if those fit your requirements or not. – jfriend00 Commented Dec 31, 2017 at 0:54
3 Answers
Reset to default 2For monjs 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 pile (by necessity because you send the source to web pages)
Old trick with Webpack https://github./minimal-xyz/minimal-webpack-nodejs-hmr .
Webpack has support for hot module replacement and Webpack also supports piling Node.js apps, and it works to bined these two features. Notice: it's designed for development, NOT for deployment.
While it's already a viable solution, I consider Webpack includes too many features which makes it quite heavy. I'm still in hope that we have lighter solution for that one day like how it's supporting HMR by default in ClojureScript.
Yes, you can use eval
to hot-swap code in Node.
let fn = () => console.log('foo');
fn(); // foo
eval(`fn = () => console.log('bar');`); // hot swap the source of fn
fn(); // bar