jprichardson / node-jsonfile

Easily read/write JSON files.
MIT License
1.2k stars 321 forks source link

Throws even if throws is set to false #47

Closed abrkn closed 7 years ago

abrkn commented 8 years ago

jsonfile.readFileSync(statePath, { throws: false })

fs.js:521
  if (typeof m === 'string')
               ^
Error: ENOENT: no such file or directory, open '/.../balance-reporting.json'
jprichardson commented 8 years ago

Yeah, I need to fix this...

Even though the docs indicate this:

throws (boolean, default: true). If JSON.parse throws an error, throw the error. If false, returns null for the object.

It's pretty confusing.

Related: https://github.com/jprichardson/node-jsonfile/pull/42

Tarabass commented 8 years ago

I got this same issue. I need to read an file and if it not exists I want to create one with an empty object. So if the result is null of jsonfile.readFileSync I can create it. But fs is throwing the error with no respect for the 'throws' option.

Maybe it's an idea to create two extra methods, exists and existsSync, and under the hood use fs.stat and fs.statSync?

ajbogh commented 7 years ago

@jprichardson, you're welcome to merge #42 if you want. I've fixed the merge conflict.

jprichardson commented 7 years ago

Closed with #42