JSON files are being deleted (cleared)


Hello, im using js server side, and time to time my json files are being cleared. They get empty, than i get error “SyntaxError: /app/welcomer.json: Unexpected end of JSON input” becouse it has no {}… Idk why is that happening.
Im doing discord bot, im loading with
const servers = require("./servers.json") in front of message event
and saving with
fs.writeFile("./servers.json", JSON.stringify(servers), (err) =>{ if(err) console.log(err); });
in message event
and there are lines of code doing with Json in message event.
Could be the error that the JSON file was not saved after loading?


To read JSON files you don’t use require, use fs.


const servers = JSON.parse(fs.readFileSync("./servers.json"))


Do you think its the problem and it wont get deleted anymore? i have 5 json files and all got deleted at same time


Yes, because it’s probably stringifying null and saving it.


Ty, ill write if the issue will repeat