# 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

Latest version **4.0.1** (published 2025-12-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-file-plus
pnpm add json-file-plus
yarn add json-file-plus
bun add json-file-plus
```

## Health

**Score 58/100 (C)** — status: stable.

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2025-12-18 |
| First published | 2013-04-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/json-file-plus) |
| Module format | ESM + CommonJS |
| Node | ^16.20.2 \|\| ^18.20.6 \|\| ^20.18.3 \|\| ^22.14.0 \|\| >= 23.8 |
| Dependencies | 1 |
| Unpacked size | 18.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 44 |
| Author | Jordan Harband |
| Maintainers | ljharb |
| Keywords | json, json file, formatting, read, write, promise |

## Links

- npm: https://www.npmjs.com/package/json-file-plus
- Repository: https://github.com/ljharb/json-file-plus
- Homepage: https://github.com/ljharb/json-file-plus#readme
- Issues: https://github.com/ljharb/json-file-plus/issues
- Funding: https://github.com/sponsors/ljharb
- npm.io page: https://npm.io/package/json-file-plus

## Dependencies (1)

- [node.extend](https://npm.io/package/node.extend.md) ^2.0.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 4.0.1 (latest) — 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
- … 17 more at https://npm.io/package/json-file-plus/versions

## README

# json-file-plus <sup>[![Version Badge][npm-version-svg]][npm-url]</sup>

[![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-image]][downloads-url]

[![npm badge][npm-badge-png]][npm-url]

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

## Example
```js
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

[npm-url]: https://npmjs.org/package/json-file-plus
[npm-version-svg]: https://versionbadg.es/ljharb/json-file-plus.svg
[deps-svg]: https://david-dm.org/ljharb/json-file-plus.svg
[deps-url]: https://david-dm.org/ljharb/json-file-plus
[dev-deps-svg]: https://david-dm.org/ljharb/json-file-plus/dev-status.svg
[dev-deps-url]: https://david-dm.org/ljharb/json-file-plus#info=devDependencies
[npm-badge-png]: https://nodei.co/npm/json-file-plus.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/json-file-plus.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/json-file-plus.svg
[downloads-url]: https://npm-stat.com/charts.html?package=json-file-plus
[codecov-image]: https://codecov.io/gh/ljharb/json-file-plus/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/ljharb/json-file-plus/
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/json-file-plus
[actions-url]: https://github.com/ljharb/json-file-plus/actions

---
_Source: https://npm.io/package/json-file-plus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
