Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load file after being modified

So, i have no idea about what I should type to get answers to those questions, I will explain my case here. I have a program, which is reading a JSON file that is being modified. The thing is, I am loading the file like the following.

const foo = require('./foo.json')

I would like to know how I could reload the file every time the constant is called so I am getting modifications in the file. I tried to use the following to do it :

const foo = JSON.parse(fs.readFileSync(fooPath))

But it doesn't look like what I want.

like image 596
Zayonx Avatar asked Aug 10 '26 08:08

Zayonx


1 Answers

Seems like you are trying to reopen the json expecting it to have modified values.

Node.js uses cache on requires, so you would need to clear the cached require

delete require.cache[require.resolve('./foo.json')]

and then require the json again.

like image 114
Francisco Garrido Avatar answered Aug 11 '26 21:08

Francisco Garrido



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!