javascript - Reload modules on the fly in Node REPL -
i testing module repl this:
repl.start({ input: process.stdin, output: process.stdout }) .context.mymodule = mymodule;
is there way reload module automatically, when change , save it, without having exit , run repl again?
you can use chokidar
module , force reload (you lose runtime context in module, should auto-reload).
var ctx = repl.start({ input: process.stdin, output: process.stdout }) .context; ctx.mymodule = require('./mymodule'); chokidar.watch('.', {ignored: /[\/\\]\./}).on('all', function(event, path) { delete require.cache['./mymodule']; ctx.mymodule = require('./mymodule'); });
if doesn't work, i'm happy play little , working solution.
edit: if doesn't garbage-collect cleanly (there open handles/listeners), leak each time reloads. may need add 'clean-exit' function mymodule
stop gracefully, , call inside watch handler.
Comments
Post a Comment