# package-config

> Get namespaced config from the closest package.json

Latest version **5.0.0** (published 2023-11-05) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 30/100 (F)** — status: abandoned.

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 5.0.0 |
| Published | 2023-11-05 |
| First published | 2015-01-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 2 |
| Unpacked size | 8.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 119 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | json, read, parse, file, fs, graceful, load, package, config, configuration, object, namespace, namespaced |

## Links

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

## Dependencies (2)

- [find-up-simple](https://npm.io/package/find-up-simple.md) ^1.0.0
- [load-json-file](https://npm.io/package/load-json-file.md) ^7.0.1

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

- 5.0.0 (latest) — 2023-11-05
- 0.1.0 — 2015-01-09

## README

# package-config

> Get namespaced config from the closest package.json

Having tool specific config in package.json reduces the amount of metafiles in your repo (there are usually a lot!) and makes the config obvious compared to hidden dotfiles like `.eslintrc`, which can end up causing confusion. [XO](https://github.com/xojs/xo), for example, uses the `xo` namespace in package.json, and [ESLint](http://eslint.org) uses `eslintConfig`. Many more tools supports this, like [AVA](https://avajs.dev), [Babel](https://babeljs.io), [nyc](https://github.com/istanbuljs/nyc), etc.

## Install

```sh
npm install package-config
```

## Usage

```json
{
	"name": "some-package",
	"version": "1.0.0",
	"unicorn": {
		"rainbow": true
	}
}
```

```js
import {packageConfig} from 'package-config';

const config = await packageConfig('unicorn');

console.log(config.rainbow);
//=> true
```

## API

It [walks up](https://github.com/sindresorhus/find-up-simple) parent directories until a `package.json` can be found, reads it, and returns the user specified namespace or an empty object if not found.

### packageConfig(namespace, options?)

Returns a `Promise` for the config.

### packageConfigSync(namespace, options?)

Returns the config.

#### namespace

Type: `string`

The package.json namespace you want.

#### options

Type: `object`

##### cwd

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

The directory to start looking up for a package.json file.

##### defaults

Type: `object`

The default config.

##### skipOnFalse

Type: `boolean`\
Default: `false`

Skip `package.json` files that have the namespaced config explicitly set to `false`.

Continues searching upwards until the next `package.json` file is reached. This can be useful when you need to support the ability for users to have nested `package.json` files, but only read from the root one, like in the case of [`electron-builder`](https://github.com/electron-userland/electron-builder/wiki/Options#AppMetadata) where you have one `package.json` file for the app and one top-level for development.

Example usage for the user:

```json
{
	"name": "some-package",
	"version": "1.0.0",
	"unicorn": false
}
```

### packageJsonPath(config)

Pass in the config returned from any of the above methods.

Returns the file path to the package.json file or `undefined` if not found.

## Related

- [read-package-up](https://github.com/sindresorhus/read-package-up) - Read the closest package.json file
- [read-pkg](https://github.com/sindresorhus/read-pkg) - Read a package.json file
- [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories

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