# write-json-file

> Stringify and write JSON to a file atomically

Latest version **7.0.0** (published 2025-09-14) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 40/100 (D)** — status: maintenance-mode.

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

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 7.0.0 |
| Published | 2025-09-14 |
| First published | 2015-09-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 4 |
| Unpacked size | 7.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 225 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | write, json, stringify, file, fs, graceful, stable, sort, newline, indent, atomic, atomically |

## Links

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

## Dependencies (4)

- [sort-keys](https://npm.io/package/sort-keys.md) ^6.0.0
- [is-plain-obj](https://npm.io/package/is-plain-obj.md) ^4.1.0
- [detect-indent](https://npm.io/package/detect-indent.md) ^7.0.1
- [write-file-atomic](https://npm.io/package/write-file-atomic.md) ^6.0.0

## 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

- 7.0.0 (latest) — 2025-09-14
- 6.0.0 — 2024-07-26
- 5.0.0 — 2021-08-14
- 4.3.0 — 2020-02-07
- 4.2.1 — 2019-11-16
- 4.2.0 — 2019-07-30
- 4.1.1 — 2019-07-01
- 4.1.0 — 2019-05-25
- 4.0.0 — 2019-05-21
- 3.2.0 — 2019-04-06
- 3.1.0 — 2019-02-12
- 3.0.2 — 2018-09-12
- 3.0.1 — 2018-09-03
- 3.0.0 — 2018-08-27
- 2.3.0 — 2017-10-11
- … 7 more at https://npm.io/package/write-json-file/versions

## README

# write-json-file

> Stringify and write JSON to a file [atomically](https://github.com/npm/write-file-atomic)

Creates directories for you as needed.

## Install

```sh
npm install write-json-file
```

## Usage

```js
import {writeJsonFile} from 'write-json-file';

await writeJsonFile('foo.json', {foo: true});
```

## API

### writeJsonFile(filePath, data, options?)

Returns a `Promise`.

### writeJsonFileSync(filePath, data, options?)

#### options

Type: `object`

##### indent

Type: `string | number | undefined`\
Default: `'\t'`

Indentation as a string or number of spaces.

Pass in `undefined` for no formatting.

If you set both this and `detectIndent`, this value will be used when the indentation cannot be detected.

##### detectIndent

Type: `boolean`\
Default: `false`

Detect indentation automatically if the file exists.

##### sortKeys

Type: `boolean | Function`\
Default: `false`

Sort the keys recursively.

Optionally pass in a [`compare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) function.

##### replacer

Type: `Function`

Passed into [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#The_replacer_parameter).

##### mode

Type: `number`\
Default: `0o666`

The [mode](https://en.wikipedia.org/wiki/File_system_permissions#Numeric_notation) used when writing the file.

## Related

- [load-json-file](https://github.com/sindresorhus/load-json-file) - Read and parse a JSON file
- [make-dir](https://github.com/sindresorhus/make-dir) - Make a directory and its parents if needed

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