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
The
throwsoption has an inconsistent behavior betweenreadJsonandreadJsonSyncwhen 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:
Outputs:
Versions:
fs-extraversion: 5.0.0