Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js reload dependencies without restarting app

Tags:

node.js

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.

like image 862
majidarif Avatar asked Oct 20 '22 05:10

majidarif


1 Answers

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.

like image 200
wbennett Avatar answered Oct 23 '22 02:10

wbennett