UNPKG

1.62 kBMarkdownView Raw
1# read-pkg
2
3> Read a package.json file
4
5## Why
6
7- [Throws more helpful JSON errors](https://github.com/sindresorhus/parse-json)
8- [Normalizes the data](https://github.com/npm/normalize-package-data#what-normalization-currently-entails)
9
10## Install
11
12```sh
13npm install read-pkg
14```
15
16## Usage
17
18```js
19import {readPackage} from 'read-pkg';
20
21console.log(await readPackage());
22//=> {name: 'read-pkg', …}
23
24console.log(await readPackage({cwd: 'some-other-directory'}));
25//=> {name: 'unicorn', …}
26```
27
28## API
29
30### readPackage(options?)
31
32Returns a `Promise<object>` with the parsed JSON.
33
34### readPackageSync(options?)
35
36Returns the parsed JSON.
37
38#### options
39
40Type: `object`
41
42##### cwd
43
44Type: `URL | string`\
45Default: `process.cwd()`
46
47Current working directory.
48
49##### normalize
50
51Type: `boolean`\
52Default: `true`
53
54[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data.
55
56### parsePackage(packageFile, options?)
57
58Parses an object or string into JSON.
59
60#### packageFile
61
62Type: `object | string`
63
64An object or a stringified object to be parsed as a package.json.
65
66#### options
67
68Type: `object`
69
70##### normalize
71
72Type: `boolean`\
73Default: `true`
74
75[Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data.
76
77## Related
78
79- [read-package-up](https://github.com/sindresorhus/read-package-up) - Read the closest package.json file
80- [write-package](https://github.com/sindresorhus/write-package) - Write a `package.json` file
81- [load-json-file](https://github.com/sindresorhus/load-json-file) - Read and parse a JSON file