readJsonSync(file, [options])

Reads a JSON file and then parses it into an object. options are the same that you’d pass to jsonFile.readFileSync.

Alias: readJSONSync()

  • file <String>
  • options <Object>

Example:

  1. const fs = require('fs-extra')
  2. const packageObj = fs.readJsonSync('./package.json')
  3. console.log(packageObj.version) // => 2.0.0

readJsonSync() can take a throws option set to false and it won’t throw if the JSON is invalid. Example:

  1. const fs = require('fs-extra')
  2. const file = '/tmp/some-invalid.json'
  3. const data = '{not valid JSON'
  4. fs.writeFileSync(file, data)
  5. const obj = fs.readJsonSync(file, { throws: false })
  6. console.log(obj) // => null