javascriptnode.jswindowsrequireread-eval-print-loop

Node REPL : Latest Script file contents are not getting fetched using require method


While using node js repl to run run require command latest file changes are not getting captured.

OS => Windows 11 Node Version => 16.6.0

Let script.js has content

global.testObject = {};
testObject.cont = "This is sample 1";

After accessing the file in node repl using require('script.js'), the testObject is available in the repl. Then after changing the file content say to

global.testObject = {};
testObject.cont = "This is an updated sample 2";

Running require('script.js') again. But the cont of testObject is not getting updated in repl.

Does anyone know the reason and how to make latest changes available in the repl using require.


Solution

  • Thanks for all the input. As a work around one can try to clear specific file entry from the require cache as below.

    delete require.cache["Absolute path to previously required file"]. 
    

    Then try to require the same file again. Which will reflect the latest changes to the file.

    Though not sure if there is any serious performance/function issues.