2020-03-07 11:45:40 +08:00
# read-pkg [![Build Status](https://travis-ci.org/sindresorhus/read-pkg.svg?branch=master)](https://travis-ci.org/sindresorhus/read-pkg)
> Read a package.json file
## Why
- [Gracefully handles filesystem issues ](https://github.com/isaacs/node-graceful-fs )
- [Throws more helpful JSON errors ](https://github.com/sindresorhus/parse-json )
- [Normalizes the data ](https://github.com/npm/normalize-package-data#what-normalization-currently-entails )
## Install
```
2020-03-31 20:57:48 +08:00
$ npm install read-pkg
2020-03-07 11:45:40 +08:00
```
## Usage
```js
const readPkg = require('read-pkg');
2020-03-31 20:57:48 +08:00
(async () => {
console.log(await readPkg());
//=> {name: 'read-pkg', …}
2020-03-07 11:45:40 +08:00
2020-03-31 20:57:48 +08:00
console.log(await readPkg({cwd: 'some-other-directory'}));
//=> {name: 'unicorn', …}
})();
2020-03-07 11:45:40 +08:00
```
## API
2020-03-31 20:57:48 +08:00
### readPkg(options?)
2020-03-07 11:45:40 +08:00
2020-03-31 20:57:48 +08:00
Returns a `Promise<object>` with the parsed JSON.
2020-03-07 11:45:40 +08:00
2020-03-31 20:57:48 +08:00
### readPkg.sync(options?)
2020-03-07 11:45:40 +08:00
Returns the parsed JSON.
2020-03-31 20:57:48 +08:00
#### options
2020-03-07 11:45:40 +08:00
2020-03-31 20:57:48 +08:00
Type: `object`
2020-03-07 11:45:40 +08:00
2020-03-31 20:57:48 +08:00
##### cwd
2020-03-07 11:45:40 +08:00
2020-03-31 20:57:48 +08:00
Type: `string` < br >
Default: `process.cwd()`
Current working directory.
2020-03-07 11:45:40 +08:00
##### normalize
Type: `boolean` < br >
Default: `true`
[Normalize ](https://github.com/npm/normalize-package-data#what-normalization-currently-entails ) the package data.
## Related
- [read-pkg-up ](https://github.com/sindresorhus/read-pkg-up ) - Read the closest package.json file
- [write-pkg ](https://github.com/sindresorhus/write-pkg ) - Write a `package.json` file
- [load-json-file ](https://github.com/sindresorhus/load-json-file ) - Read and parse a JSON file
2020-03-31 20:57:48 +08:00
---
2020-03-07 11:45:40 +08:00
2020-03-31 20:57:48 +08:00
< div align = "center" >
< b >
< a href = "https://tidelift.com/subscription/pkg/npm-read-pkg?utm_source=npm-read-pkg&utm_medium=referral&utm_campaign=readme" > Get professional support for this package with a Tidelift subscription< / a >
< / b >
< br >
< sub >
Tidelift helps make open source sustainable for maintainers while giving companies< br > assurances about security, maintenance, and licensing for their dependencies.
< / sub >
< / div >