0

If I have a program as follows to modify a JSON file:

var fs = require('fs');
var dt = require('./dataWrite.json');
console.log("before",dt);
fs.readFile('./data.json', 'utf8', (err, data) => {
    if (err) {
        throw err;
    }
    else {
        fs.writeFileSync('./dataWrite.json', data);
    }
});

console.log("after",dt);

The console for the before and after gives me the same results. The data in the file is modified as expected though. Is there a way to always have the latest state of the file in your program?

Side question: the following code doesn't modify the files at all, I wasn't able to figure why

var fs = require('fs');
var dt = fs.readFileSync('./dataTest.json', 'utf8', function (err, data) {
if (err) {
    throw err;
    }
});
console.log('before', dt);
fs.readFileSync('./data.json', 'utf8', (err, data) => {
    if (err) {
        throw err;
    }
    fs.writeFileSync('./dataTest.json', data);
    console.log('data', data);
});
console.log("after", dt);
Vayun
  • 95
  • 1
  • 10
  • Module loading is cached, so won’t take account of file system changes. You can modify the loaded object though, and that will persist elsewhere (assuming by reference semantics in the loaded object). – Dave Meehan Jun 12 '22 at 09:56

1 Answers1

0

It's important here to distinguish between synchronous and asynchronous logic.

Since you are using require to read in the json file, the value of the file when the program executes is read in synchronously within dt, and read in once at the beginning of the program only.

When you use the fs.readFile API, you'll notice that it is an asynchronous API and that it requires you to provide a callback to handle the file's data. This means that any execution within it is handled at a later date.

As such, your before and after code will just print the same contents.

If you console.log(dt) after executing fs.writeFileSync you will still see the old value since dt is holding the old value known at the beginning of the program and not the latest value, but if you update the variable to the contents of the file after rereading the file, then you would see the latest contents.

e.g.

...
fs.writeFileSync('./dataWrite.json', data);
dt = fs.readFileSync('./dataWrite.json', 'utf8');
console.log(dt);
...

See fs.readFileSync.

Kaushik Shankar
  • 5,491
  • 4
  • 30
  • 36
  • Thanks for the response. Just to clarify, will I have to read the file every time I make a change? – Vayun Jun 12 '22 at 10:44
  • I don't think you need to—you already have the `data` available, so you don't need to reread it if you know the file wrote successfully. If on the other hand you don't know that the file wrote successfully, the `readFileSync` API would have thrown an error, which would have broke out of the normal code flow anyway. In the code sample above, I just wanted to clarify that `dt` is out of date unless you update it manually, but you shouldn't have to do that at all since you have the new value within `data` already. – Kaushik Shankar Jun 12 '22 at 10:48
  • That's fine but if I do need the latest state of the file, how do I do that? – Vayun Jun 12 '22 at 18:32