I am writing an mmo server and someone was able to create a server that when he makes changes to the library files it automatically reflects on the next function call without restarting the server itself.
(he watches the directory for changes and it automatically reloads the libs without restarting)
He talks about something called a VM module of node (experimental), but I would like to ask if there are any modules that does this and what are the implications of doing this.
Few research on VM shows it is unstable. But how is this done? Writing server update without restarting the app can really help with development as you won't need to go back to the very start each time there is a problem, and just need to change a small thing, and you can change code and try it immediately on the client.
This is not browser related, its a MMORPG client server implementation.
So really, how is this possible?
Note: I know nodemon, supervisor, pm2... This all RESTART the app so it kills, and spawns it again.
I was trying something without using VM
var watch = require('watch')
// load data
var data = require('../lib/stuff.js')
watch.createMonitor('/...', function (monitor) {
monitor.on("changed", function (f, curr, prev) {
data = require('../lib/stuff.js')
console.log(data)
})
})
console.log(data)
Which should if I add functions to the stuff.js
theoretically it will get reloaded and should the new functions be now available? WITHOUT re running the main script?
Found out that the problem of my code above won't work because Node.JS caches the requires.
Checkout hot swap: https://github.com/rlidwka/node-hotswap/blob/master/README.md
Or check-out hotload (more recent): https://github.com/jan-swiecki/node-hotload
I think it does what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With