writeJson(file, object, [options, callback])

Writes an object to a JSON file.

Alias: writeJSON()

  • file <String>
  • object <Object>
  • options <Object>
    • spaces <Number|String> Number of spaces to indent; or a string to use for indentation (i.e. pass '\t' for tab indentation). See the docs for more info.
    • EOL <String> Set EOL character. Default is \n.
    • replacer JSON replacer
    • Also accepts fs.writeFile options
  • callback <Function>

Example:

  1. const fs = require('fs-extra')
  2. fs.writeJson('./package.json', {name: 'fs-extra'}, err => {
  3. if (err) return console.error(err)
  4. console.log('success!')
  5. })
  6. // With Promises
  7. fs.writeJson('./package.json', {name: 'fs-extra'})
  8. .then(() => {
  9. console.log('success!')
  10. })
  11. .catch(err => {
  12. console.error(err)
  13. })

See also: outputJson()