包详细信息

jsonfile

jprichardson343.9mMIT6.2.0

Easily read/write JSON files.

read, write, file, json

自述文件

Node.js - jsonfile

Easily read/write JSON files in Node.js. Note: this module cannot be used in the browser.

npm Package linux build status windows Build status

Standard JavaScript

Why?

Writing JSON.stringify() and then fs.writeFile() and JSON.parse() with fs.readFile() enclosed in try/catch blocks became annoying.

Installation

npm install --save jsonfile

API


readFile(filename, [options], callback)

options (object, default undefined): Pass in any fs.readFile options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If JSON.parse throws an error, pass this error to the callback. If false, returns null for the object.
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file, function (err, obj) {
  if (err) console.error(err)
  console.dir(obj)
})

You can also use this method with promises. The readFile method will return a promise if you do not pass a callback function.

const jsonfile = require('jsonfile')
const file = '/tmp/data.json'
jsonfile.readFile(file)
  .then(obj => console.dir(obj))
  .catch(error => console.error(error))

readFileSync(filename, [options])

options (object, default undefined): Pass in any fs.readFileSync options or set reviver for a JSON reviver.

  • throws (boolean, default: true). If an error is encountered reading or parsing the file, throw the error. If false, returns null for the object.
const jsonfile = require('jsonfile')
const file = '/tmp/data.json'

console.dir(jsonfile.readFileSync(file))

writeFile(filename, obj, [options], callback)

options: Pass in any fs.writeFile options or set replacer for a JSON replacer. Can also pass in spaces, or override EOL string or set finalEOL flag as false to not save the file with EOL at the end.

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, function (err) {
  if (err) console.error(err)
})

Or use with promises as follows:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj)
  .then(res => {
    console.log('Write complete')
  })
  .catch(error => console.error(error))

formatting with spaces:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2 }, function (err) {
  if (err) console.error(err)
})

overriding EOL:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
  if (err) console.error(err)
})

disabling the EOL at the end of file:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { spaces: 2, finalEOL: false }, function (err) {
  if (err) console.log(err)
})

appending to an existing JSON file:

You can use fs.writeFile option { flag: 'a' } to achieve this.

const jsonfile = require('jsonfile')

const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }

jsonfile.writeFile(file, obj, { flag: 'a' }, function (err) {
  if (err) console.error(err)
})

writeFileSync(filename, obj, [options])

options: Pass in any fs.writeFileSync options or set replacer for a JSON replacer. Can also pass in spaces, or override EOL string or set finalEOL flag as false to not save the file with EOL at the end.

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj)

formatting with spaces:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2 })

overriding EOL:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2, EOL: '\r\n' })

disabling the EOL at the end of file:

const jsonfile = require('jsonfile')

const file = '/tmp/data.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { spaces: 2, finalEOL: false })

appending to an existing JSON file:

You can use fs.writeFileSync option { flag: 'a' } to achieve this.

const jsonfile = require('jsonfile')

const file = '/tmp/mayAlreadyExistedData.json'
const obj = { name: 'JP' }

jsonfile.writeFileSync(file, obj, { flag: 'a' })

License

(MIT License)

Copyright 2012-2016, JP Richardson jprichardson@gmail.com

更新日志

6.2.0 / 2025-08-12

  • Add support for importing as ESM named imports (e.g. import { readFileSync } from 'jsonfile') (#162)

6.1.0 / 2020-10-31

  • Add finalEOL option to disable writing final EOL (#115, #137)
  • Update dependency (#138)

6.0.1 / 2020-03-07

  • Update dependency (#130)
  • Fix code style (#129)

6.0.0 / 2020-02-24

  • BREAKING: Drop support for Node 6 & 8 (#128)
  • BREAKING: Do not allow passing null as options to readFile() or writeFile() (#128)
  • Refactor internals (#128)

5.0.0 / 2018-09-08

  • BREAKING: Drop Node 4 support
  • BREAKING: If no callback is passed to an asynchronous method, a promise is now returned (#109)
  • Cleanup docs

4.0.0 / 2017-07-12

  • BREAKING: Remove global spaces option.
  • BREAKING: Drop support for Node 0.10, 0.12, and io.js.
  • Remove undocumented passParsingErrors option.
  • Added EOL override option to writeFile when using spaces. #89

3.0.1 / 2017-07-05

  • Fixed bug in writeFile when there was a serialization error & no callback was passed. In previous versions, an empty file would be written; now no file is written.

3.0.0 / 2017-04-25

  • Changed behavior of throws option for readFileSync; now does not throw filesystem errors when throws is false

2.4.0 / 2016-09-15

Changed

  • added optional support for graceful-fs [#62]

2.3.1 / 2016-05-13

  • fix to support BOM. #45

2.3.0 / 2016-04-16

  • add throws to readFile(). See #39
  • add support for any arbitrary fs module. Useful with mock-fs

2.2.3 / 2015-10-14

2.2.2 / 2015-09-16

2.2.1 / 2015-06-25

2.2.0 / 2015-06-25

  • added options.spaces to writeFile() and writeFileSync()

2.1.2 / 2015-06-22

2.1.1 / 2015-06-19

2.1.0 / 2015-06-19

  • cleanup: JavaScript Standard Style, rename files, dropped terst for assert
  • methods now support JSON revivers/replacers

2.0.1 / 2015-05-24

2.0.0 / 2014-07-28

  • added \n to end of file on write. #14
  • added options.throws to readFileSync()
  • dropped support for Node v0.8

1.2.0 / 2014-06-29

  • removed semicolons
  • bugfix: passed options to fs.readFile and fs.readFileSync. This technically changes behavior, but changes it according to docs. #12

1.1.1 / 2013-11-11

  • fixed catching of callback bug (ffissore / #5)

1.1.0 / 2013-10-11

  • added options param to methods, (seanodell / #4)

1.0.1 / 2013-09-05

  • removed homepage field from package.json to remove NPM warning

1.0.0 / 2013-06-28

  • added .npmignore, #1
  • changed spacing default from 4 to 2 to follow Node conventions

0.0.1 / 2012-09-10

  • Initial release.