You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

readJson-sync.md 797 B

3 年之前
123456789101112131415161718192021222324252627282930313233
  1. # readJsonSync(file, [options])
  2. Reads a JSON file and then parses it into an object. `options` are the same
  3. that you'd pass to [`jsonFile.readFileSync`](https://github.com/jprichardson/node-jsonfile#readfilesyncfilename-options).
  4. **Alias:** `readJSONSync()`
  5. - `file` `<String>`
  6. - `options` `<Object>`
  7. ## Example:
  8. ```js
  9. const fs = require('fs-extra')
  10. const packageObj = fs.readJsonSync('./package.json')
  11. console.log(packageObj.version) // => 2.0.0
  12. ```
  13. ---
  14. `readJsonSync()` can take a `throws` option set to `false` and it won't throw if the JSON is invalid. Example:
  15. ```js
  16. const fs = require('fs-extra')
  17. const file = '/tmp/some-invalid.json'
  18. const data = '{not valid JSON'
  19. fs.writeFileSync(file, data)
  20. const obj = fs.readJsonSync(file, { throws: false })
  21. console.log(obj) // => null
  22. ```