I have a backend where every time it gets called, it imports a local JSON file and prints its content, what it also does at the end is dynamically modifying the JSON file at runtime. However, an issue I seem to encounter is that every time it prints the content, it is always the content at the first time the module was imported, instead of the content after the many times it was dynamically modified. To better illustrate, my JSON looks like the following
{
"number": 1
}
My backend looks like
app.use('/change', async (req, res) => {
const num = require('number.json').number;
console.log(num);
await modifyJsonFile(
path.join(__dirname, 'number.json'),
{
number: num + 1
}
);
res.end();
}
Now the interesting thing happens. When I call it the first time, it would print "1", and the number in the JSON file changes to "2". When I call it the second time, it would still print "1", even though the property in the JSON file is "2". My guess for the issue is that the backend has always been using the JSON module the first time it was imported. Any possible solutions? especially on re-importing the modified module, or alternative walk-around? Thanks.