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
|
13 | npm install read-pkg
|
14 | ```
|
15 |
|
16 | ## Usage
|
17 |
|
18 | ```js
|
19 | import {readPackage} from 'read-pkg';
|
20 |
|
21 | console.log(await readPackage());
|
22 | //=> {name: 'read-pkg', …}
|
23 |
|
24 | console.log(await readPackage({cwd: 'some-other-directory'}));
|
25 | //=> {name: 'unicorn', …}
|
26 | ```
|
27 |
|
28 | ## API
|
29 |
|
30 | ### readPackage(options?)
|
31 |
|
32 | Returns a `Promise<object>` with the parsed JSON.
|
33 |
|
34 | ### readPackageSync(options?)
|
35 |
|
36 | Returns the parsed JSON.
|
37 |
|
38 | #### options
|
39 |
|
40 | Type: `object`
|
41 |
|
42 | ##### cwd
|
43 |
|
44 | Type: `URL | string`\
|
45 | Default: `process.cwd()`
|
46 |
|
47 | Current working directory.
|
48 |
|
49 | ##### normalize
|
50 |
|
51 | Type: `boolean`\
|
52 | Default: `true`
|
53 |
|
54 | [Normalize](https://github.com/npm/normalize-package-data#what-normalization-currently-entails) the package data.
|
55 |
|
56 | ### parsePackage(packageFile, options?)
|
57 |
|
58 | Parses an object or string into JSON.
|
59 |
|
60 | #### packageFile
|
61 |
|
62 | Type: `object | string`
|
63 |
|
64 | An object or a stringified object to be parsed as a package.json.
|
65 |
|
66 | #### options
|
67 |
|
68 | Type: `object`
|
69 |
|
70 | ##### normalize
|
71 |
|
72 | Type: `boolean`\
|
73 | Default: `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
|