Skip to content

Inconsistency between readJson and readJsonSync when file does not exist #542

@LeoFrachet

Description

@LeoFrachet

The throws option has an inconsistent behavior between readJson and readJsonSync when the file do not exist:

  • fs.readJsonSync('path', { throws: false}); do not throw if the file do not exist;
  • fs.readJson('path', { throws: false}); do throw if the file do not exist.

I would expect a consistent behavior between the two.

Test code:

console.log('Trying to load a non existing file using readJson/readJsonSync:');

try {
  fs.readJsonSync('I do not exist.json');
  console.log('readJsonSync does NOT throw.');
} catch (e) {
  console.log('readJsonSync throws.');
}

try {
  fs.readJsonSync('I do not exist.json', { throws: false });
  console.log('readJsonSync (with throws = false) does NOT throw.');
} catch (e) {
  console.log('readJsonSync (with throws = false) throws.');
}

fs.readJson('I do not exist.json').then(() => {
  console.log('readJson does NOT throw.')
}).catch((e) => {
  console.log('readJson throws');
});

fs.readJson('I do not exist.json', { throws: false }).then(() => {
  console.log('readJson (with throws = false) does NOT throw.')
}).catch((e) => {
  console.log('readJson (with throws = false) throws');
});

Outputs:

Trying to load a non existing file using readJson/readJsonSync:
readJsonSync throws.
readJsonSync (with throws = false) does NOT throw.
readJson throws
readJson (with throws = false) throws

Versions:

  • Operating System: macOS 10.13.2 (17C88)
  • Node.js version: 8.0.0
  • fs-extra version: 5.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions