# write-package

> Write a package.json file

Latest version **7.2.0** (published 2025-09-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install write-package
pnpm add write-package
yarn add write-package
bun add write-package
```

## Health

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

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

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 7.2.0 |
| Published | 2025-09-11 |
| First published | 2023-11-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 5 |
| Unpacked size | 21.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 89 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | json, write, stringify, file, fs, graceful, package |

## Links

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

## Dependencies (5)

- [read-pkg](https://npm.io/package/read-pkg.md) ^9.0.1
- [sort-keys](https://npm.io/package/sort-keys.md) ^5.0.0
- [type-fest](https://npm.io/package/type-fest.md) ^4.23.0
- [deepmerge-ts](https://npm.io/package/deepmerge-ts.md) ^7.1.0
- [write-json-file](https://npm.io/package/write-json-file.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.2.0 (latest) — 2025-09-11
- 7.1.0 — 2024-07-26
- 7.0.1 — 2024-02-26
- 7.0.0 — 2023-11-04

## README

# write-package

> Write a `package.json` file

Writes atomically and creates directories for you as needed. Sorts dependencies when writing. Preserves the indentation if the file already exists.

## Install

```sh
npm install write-package
```

## Usage

```js
import path from 'node:path';
import {writePackage} from 'write-package';

await writePackage({foo: true});
console.log('done');

await writePackage(path.join('unicorn', 'package.json'), {foo: true});
console.log('done');
```

## API

### writePackage(path?, data, options?)

Returns a `Promise` that resolves when the `package.json` file has been written.

### writePackageSync(path?, data, options?)

#### path

Type: `string`\
Default: `process.cwd()`

The path to where the `package.json` file should be written or its directory.

#### data

Type `object`

JSON data to write to the `package.json` file.

#### options

Type: `object`

See [Options](#options-4).

### updatePackage(path?, data, options?)

Returns a `Promise` that resolves when the `package.json` file has been updated.

### updatePackageSync(path?, data, options?)

```js
import {updatePackage} from 'write-package';

await updatePackage({foo: true});
//=> { "foo": true }

await updatePackage({foo: false, bar: true});
//=> { "foo": false, "bar": true }
```

#### path

Type: `string`\
Default: `process.cwd()`

The path to where the `package.json` file should be written or its directory.

#### data

Type `object`

JSON data to write to the `package.json` file. If the file already exists, existing fields will be merged with the values in `data`.

#### options

Type: `object`

See [Options](#options-4).

### addPackageDependencies(path?, dependencies, options?)

Returns a `Promise` that resolves when the `package.json` file has been written.

### addPackageDependenciesSync(path?, dependencies, options?)

```js
import {writePackage, addPackageDependencies} from 'write-package';

await writePackage({foo: true});
//=> { "foo": true }

await addPackageDependencies({foo: '1.0.0'});
//=> { "foo": true, "dependencies": { "foo": "1.0.0" } }

await addPackageDependencies({dependencies: {foo: '1.0.0'}, devDependencies: {bar: '1.0.0'}});
//=> { "foo": true, "dependencies": { "foo": "1.0.0" }, "devDependencies": { "bar": "1.0.0" } }
```

#### path

Type: `string`\
Default: `process.cwd()`

The path to where the `package.json` file should be written or its directory.

#### dependencies

Type: `Record<string, string> | Partial<Record<'dependencies' | 'devDependencies' | 'optionalDependencies' | 'peerDependencies', Record<string, string>>>`

Dependencies to add to the `package.json` file.

#### options

Type: `object`

See [Options](#options-4).

### removePackageDependencies(path?, dependencies, options?)

Returns a `Promise` that resolves when the `package.json` file has been written. Does not throw if the file does not exist.

### removePackageDependenciesSync(path?, dependencies, options?)

```js
import {writePackage, removePackageDependencies} from 'write-package';

await writePackage({foo: true, dependencies: {foo: '1.0.0'}, devDependencies: {bar: '1.0.0'}});
//=> { "foo": true, "dependencies": { "foo": "1.0.0" }, "devDependencies": { "bar": "1.0.0" } }

await removePackageDependencies(['foo']);
//=> { "foo": true, "devDependencies": { "bar": "1.0.0" } }

await removePackageDependencies({devDependencies: ['bar']});
//=> { "foo": true }
```

#### path

Type: `string`\
Default: `process.cwd()`

The path to where the `package.json` file should be written or its directory.

#### dependencies

Type `string[] | Partial<Record<'dependencies' | 'devDependencies' | 'optionalDependencies' | 'peerDependencies', string[]>>`

Dependencies to remove from the `package.json` file.

#### options

Type: `object`

See [Options](#options-4).

### Options

##### indent

Type: `string | number`\
Default: Auto-detected or `'\t'`

The indentation to use for new files.

Accepts `'\t'` for tab indentation or a number of spaces.

If the file already exists, the existing indentation will be used.

##### detectIndent

Type: `boolean`\
Default: `true`

Detect indentation automatically if the file exists.

##### normalize

Type: `boolean`\
Default: `true`

Remove empty `dependencies`, `devDependencies`, `optionalDependencies` and `peerDependencies` objects.

## Related

- [read-pkg](https://github.com/sindresorhus/read-pkg) - Read a `package.json` file
- [write-json-file](https://github.com/sindresorhus/write-json-file) - Stringify and write JSON to a file atomically

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