Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/read-pkg
Read a package.json file
https://github.com/sindresorhus/read-pkg
Last synced: 7 days ago
JSON representation
Read a package.json file
- Host: GitHub
- URL: https://github.com/sindresorhus/read-pkg
- Owner: sindresorhus
- License: mit
- Created: 2015-09-02T08:41:45.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2023-11-16T08:39:27.000Z (about 1 year ago)
- Last Synced: 2025-02-01T10:06:01.526Z (14 days ago)
- Language: JavaScript
- Size: 40 KB
- Stars: 166
- Watchers: 7
- Forks: 24
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-nodejs - read-pkg - Read a package.json file. ![](https://img.shields.io/github/stars/sindresorhus/read-pkg.svg?style=social&label=Star) (Repository / NPM)
README
# read-pkg
> Read a package.json file
## Why
- [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
```sh
npm install read-pkg
```## Usage
```js
import {readPackage} from 'read-pkg';console.log(await readPackage());
//=> {name: 'read-pkg', …}console.log(await readPackage({cwd: 'some-other-directory'}));
//=> {name: 'unicorn', …}
```## API
### readPackage(options?)
Returns a `Promise` with the parsed JSON.
### readPackageSync(options?)
Returns the parsed JSON.
#### options
Type: `object`
##### cwd
Type: `URL | string`\
Default: `process.cwd()`Current working directory.
##### normalize
Type: `boolean`\
Default: `true`[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data.
### parsePackage(packageFile, options?)
Parses an object or string into JSON.
#### packageFile
Type: `object | string`
An object or a stringified object to be parsed as a package.json.
#### options
Type: `object`
##### normalize
Type: `boolean`\
Default: `true`[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data.
## Related
- [read-package-up](https://github.com/sindresorhus/read-package-up) - Read the closest package.json file
- [write-package](https://github.com/sindresorhus/write-package) - Write a `package.json` file
- [load-json-file](https://github.com/sindresorhus/load-json-file) - Read and parse a JSON file