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

MIT 32 个版本
安装
npm install json-file-plus
yarn add json-file-plus
pnpm add json-file-plus
bun add json-file-plus
README

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

版本列表
4.0.1 2025-12-18
4.0.0 2025-02-14
3.3.2 2025-02-06
3.3.1 2018-03-21
3.3.0 2016-03-22
3.2.0 2015-10-18
3.1.0 2015-10-18
3.0.1 2015-08-19
3.0.0 2015-05-03
2.0.0 2014-09-26
1.0.7 2014-09-03
1.0.6 2014-09-01
1.0.5 2014-08-28
1.0.4 2014-08-26
1.0.3 2014-08-26
1.0.2 2014-08-13
1.0.1 2014-08-11
1.0.0 2014-08-07
0.2.3 2013-12-07
0.2.2 2013-10-15
0.2.1 2013-09-17
0.2.0 2013-08-21
0.1.1 2013-08-20
0.1.0 2013-08-14
0.0.8 2013-04-25
0.0.7 2013-04-18
0.0.6 2013-04-17
0.0.5 2013-04-14
0.0.4 2013-04-09
0.0.3 2013-04-07
0.0.2 2013-04-03
0.0.1 2013-04-02