Node.js: how to reload module

There are some answers here as suggested in the comments.

However they are not REPL friendly, and might even use extra modules.

Here is a one line solution that you can paste in your REPL, inspired by the discussion on the other question:

function nocache(module) {require("fs").watchFile(require("path").resolve(module), () => {delete require.cache[require.resolve(module)]})}

The function will delete your module from the cache each time the file changes.
To use it, just paste it in the REPL, call nocache("d:/myapp.js"), then use require normally.

> function nocache(module) {require("fs").watchFile(require("path").resolve(module), () => {delete require.cache[require.resolve(module)]})}
> nocache("d:/myapp.js");
> var myapp = require("d:/myapp.js");
......
> myapp = require("d:/myapp.js");
....

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)