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
.
2
Answers
Thanks for all the input. As a work around one can try to clear specific file entry from the require cache as below.
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.
First time you require(‘script.js) the script is executed. Second time you require(‘script.js’) it will not execute it. This is the way nodeJs works. if your script exports something, the first time you require it will be cached and the second be recovered from the cache.