Skip to content

fs.writeFileSync not writing but doesn't give any errors. #2116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
naenaeMaster69 opened this issue Aug 11, 2019 · 2 comments
Closed

fs.writeFileSync not writing but doesn't give any errors. #2116

naenaeMaster69 opened this issue Aug 11, 2019 · 2 comments

Comments

@naenaeMaster69
Copy link

I try using this to write the values of my money file, but when I write it it doesn't save but it also doesn't give out an error. It worked on other projects that are running the same versions and on the same OS, but for some reason this doesn't work.

// Other requires that aren't related

const fs = require("fs");

// Other code that isn't related

money[message.author.id] = {

            bank: 1000,
            cash: 0

 };

fs.writeFileSync("../money.json", JSON.stringify(money), (err) => {

       if (err) console.log(err)

});

The money.json file currently looks like this

{}
@Hakerh400
Copy link

Unable to reproduce the issue. The following code:

const fs = require('fs');
const money = {};
const message = {author: {id: 0}};
money[message.author.id] = {
  bank: 1000,
  cash: 0,
};
fs.writeFileSync('../money.json', JSON.stringify(money));

properly creates money.json file in the parent directory and writes the following content to it:

{"0":{"bank":1000,"cash":0}}

There is not enough information in your code snippet to reliably tell what the problem is, but the problem could be among these:

  • fs.writeFileSync does not take a callback. If an error occurs, it will throw the error immediately, since it is synchronous. Either change it to fs.writeFile, or remove the callback.
  • Where is money defined and how exactly the definition of that variable look like?
  • Are you sure the assignment money[message.author.id] = ... happens synchronously, or at least before fs.writeFileSync call?
  • Note that '../money.json' resolves relatively to the current working directory process.cwd(), rather to the current script directory __dirname. Are you sure you're looking at the correct file?

One possible mistake that you may be doing is this:

const fs = require('fs');
const money = {};
const someAsyncStuff = f => setTimeout(f);
someAsyncStuff(() => {
  const message = {author: {id: 0}};
  money[message.author.id] = {
    bank: 1000,
    cash: 0,
  };
});
fs.writeFileSync('../money.json', JSON.stringify(money));

where someAsyncStuff can be any function that performs some asynchronous actions and then calls the provided callback (it can be selecting from a database, reading from a file, fetching remote URL, etc).

If you share the whole code, the problem will be revealed more easily.

@naenaeMaster69
Copy link
Author

@Hakerh400 I tried fs.writeFile and it fixed it. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants