json-file-plus
[![github actions][actions-image]][actions-url] [![coverage][codecov-image]][codecov-url] [![dependency status][deps-svg]][deps-url] [![dev dependency status][dev-deps-svg]][dev-deps-url] [![License][license-image]][license-url] [![Downloads][downloads-im
Last updated a year ago by ljharb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install json-file-plus 
SYNC missed versions from official npm registry.

json-file-plus Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

A module to read from and write to JSON files, without losing formatting, to minimize diffs.

Example

const jsonFile = require('json-file-plus');
const path = require('path');
const fs = require('fs');
const assert = require('assert');

const filename = path.join(process.cwd(), 'package.json');

const originalContents = String(fs.readFileSync(filename));

jsonFile(filename).then((file) => {
	file.data; // Direct access to the data from the file
	file.format; // extracted formatting data. change at will.

	file.get('version'); // get top-level keys. returns a Promise
	file.get(); // get entire data. returns a Promise

	/* pass any plain object into "set" to merge in a deep copy */
	/* please note: references will be broken. */
	/* if a non-plain object is passed, will throw a TypeError. */
	file.set({
		foo: 'bar',
		bar: {
			baz: true,
		},
	});

	file.remove('description'); // remove a specific key-value pair. returns a Promise

	/* change the filename if desired */
	file.filename = path.join(process.cwd(), 'new-package.json');

	/* Save the file, preserving formatting. returns a Promise. */
	file.save().then(() => {
		console.log('success!');

		const finalContents = String(fs.readFileSync(filename));
		assert.equal(originalContents, finalContents);
	}).catch((err) => {
		console.log('error!', err);
		process.exitCode = 1;
	}).finally(() => {
		fs.writeFileSync(filename, originalContents, { encoding: 'utf8' });
	});
});

Tests

Simply run npm test in the repo

Current Tags

  • 4.0.1                                ...           latest (a year ago)

32 Versions

  • 4.0.1                                ...           a year ago
  • 4.0.0                                ...           2 years ago
  • 3.3.2                                ...           2 years ago
  • 3.3.1                                ...           9 years ago
  • 3.3.0                                ...           11 years ago
  • 3.2.0                                ...           11 years ago
  • 3.1.0                                ...           11 years ago
  • 3.0.1                                ...           11 years ago
  • 3.0.0                                ...           12 years ago
  • 2.0.0 [deprecated]           ...           12 years ago
  • 1.0.7 [deprecated]           ...           12 years ago
  • 1.0.6                                ...           12 years ago
  • 1.0.5                                ...           12 years ago
  • 1.0.4                                ...           12 years ago
  • 1.0.3                                ...           12 years ago
  • 1.0.2                                ...           12 years ago
  • 1.0.1 [deprecated]           ...           12 years ago
  • 1.0.0                                ...           12 years ago
  • 0.2.3 [deprecated]           ...           13 years ago
  • 0.2.2                                ...           13 years ago
  • 0.2.1                                ...           13 years ago
  • 0.2.0                                ...           13 years ago
  • 0.1.1 [deprecated]           ...           13 years ago
  • 0.1.0                                ...           13 years ago
  • 0.0.8 [deprecated]           ...           14 years ago
  • 0.0.7                                ...           14 years ago
  • 0.0.6                                ...           14 years ago
  • 0.0.5                                ...           14 years ago
  • 0.0.4                                ...           14 years ago
  • 0.0.3                                ...           14 years ago
  • 0.0.2                                ...           14 years ago
  • 0.0.1                                ...           14 years ago
Maintainers (1)
Downloads
Total 328
Today 0
This Week 49
This Month 287
Last Day 8
Last Week 61
Last Month 13
Dependencies (1)
Dev Dependencies (13)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE