Does fs.readFileSync
of node.js cache files that's been read?
I don't think it does because doc doesn't say so, but my code is behaving that way..
Relevant part of code
// after mainPath file is altered, it's not reflected until I restart node.js
var scriptString = fs.readFileSync(mainPath);
var app = vm.createScript(scriptString, mainPath);
// Run the app
app.runInContext(context);
Per the node documentation, modules are cached after the first time they are loaded (loaded is synonymous with 'required'). They are placed in the require. cache . This means that every future require for a previously loaded module throughout a program will load the same object that was loaded by the first require.
D:\myproject\subfoldername> node helloworld.js It is very easy.. Go to your command line. navigate to the file location.. then simply run the node helloworld.
To get the contents of a file as a string, we can use the readFileSync() or readFile() functions from the native filesystem ( fs ) module in Node. js. The readFileSync() function is used to synchronously read the contents from a file which further blocks the execution of code in Nodejs.
Reads are buffered during a given read operation. E.g. when you ask to read a byte, it will likely read many more bytes than a single byte into a buffer and then return that single byte to you. But beyond that, there is no caching built into node.js from one read of the file to another. And, if you're using readFileSync()
to read the whole file at once, this buffering wouldn't affect you.
The OS itself will do caching underneath node.js, but that is usually write-through caching which is designed to save disk reads, but never have stale data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With