# write-pkg

> Write a package.json file

Latest version **7.0.0** (published 2023-11-04) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

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

## Links

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

## Dependencies (5)

- [read-pkg](https://npm.io/package/read-pkg.md) ^8.1.0
- [sort-keys](https://npm.io/package/sort-keys.md) ^5.0.0
- [type-fest](https://npm.io/package/type-fest.md) ^4.6.0
- [deepmerge-ts](https://npm.io/package/deepmerge-ts.md) ^5.1.0
- [write-json-file](https://npm.io/package/write-json-file.md) ^5.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) — 2023-11-04
- 6.0.1 — 2023-11-04
- 6.0.0 — 2023-07-14
- 5.1.0 — 2022-03-20
- 5.0.0 — 2021-08-14
- 4.0.0 — 2019-04-29
- 3.2.0 — 2018-06-07
- 3.1.0 — 2017-06-27
- 3.0.1 — 2017-04-17
- 3.0.0 — 2017-04-17
- 2.1.0 — 2017-03-04
- 2.0.0 — 2016-10-11
- 1.0.0 — 2015-09-02

## README

# write-pkg

> 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-pkg
```

## Usage

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

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-pkg';

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-pkg';

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-pkg';

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.

##### normalize

Type: `boolean`\
Default: `true`

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

## write-pkg for enterprise

Available as part of the Tidelift Subscription.

The maintainers of write-pkg and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-write-pkg?utm_source=npm-write-pkg&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)

## 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-pkg · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
