aishwat singh aishwat singh - 3 months ago 7
Javascript Question

Does require in node js, updates value at run time?

I have something like

var some_data=require('../objects/some_data.json');

function(){
//something here to change data of some_data.json - fs.write
//vl this affect value of some_data at this point ?
}


Does require keep active reference to files OR just reads it once while requiring ?

If it keeps active ref , how can i retain old value of some_data

mmm mmm
Answer

It loads the file, how it is on start. It doesn't take any changes you are doing to the json file.

For example, run this

var b = require("./a.json");
for (var i = 0; i< 1000000; i++) {
    console.log(b.a);
}

with this json a.json

{
  "a": "b"
}

And change the value whilst in the loop - or delete the file. It will still work.