JSON files are being deleted (cleared)


#1

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?


#2

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

Example:

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

#3

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


#4

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


#5

Ty, ill write if the issue will repeat