JSON fs write file

When ever I try to use fs, to save my coins or stuff it doesn’t save in the coins.json file.

Could you show us your code?

How are you trying to write the file contents? Can you post some of your logic (code).

let coins = require("./coins.json")

coins[message.author.id] = {
coins: 0


let coinAmt = Math.floor(Math.random() * 15) + 1;
let baseAmt = Math.floor(Math.random() * 15) + 1;
console.log(${coinAmt} : ${baseAmt})
if(coinAmt === baseAmt) {
coins[message.author.id] = {
coins: coins[message.author.id].coins + coinAmt
fs.writeFile("./coins.json", JSON.stringify(coins), (err) => {
if(err) console.log(err)
} )

I do have fs installed and reqiured

Does the file save? Like do the file exist? Is the problem that the file doesn’t save new data?

The file exists, not sure what you mean by does it save.

The issue here is that the require function is caching your data. After using require on your json file you need to remove the cache entry. Add the code below under the line where you require the coins.json file:

delete require.cache[require.resolve("./coins.json")];

okay I’ll add it, is there anything else?

I don’t think so, come back to me if there is any other issues :wink:

It doesn’t log anything in the coins.json

Can you send me an invite to your project in DMs and I’ll try to debug the issue more thoroughly?

I’m just going to use mysql or some other database, json storage is really bad anyways.

1 Like

Thanks for your help ihack2712

No worries, hope you find a good solution :slight_smile:

Try using refresh in the terminal then check. Glitch doesnt the editor update when a file gets changed from the terminal or code