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