3 This is the thing that npm uses to read package.json files. It
4 validates some stuff, and loads some default things.
6 It keeps a cache of the files you've read, so that you don't end
7 up reading the same package.json file multiple times.
9 Note that if you just want to see what's literally in the package.json
10 file, you can usually do `var data = require('some-module/package.json')`.
12 This module is basically only needed by npm, but it's handy to see what
13 npm will see when it looks at your package.
18 var readJson = require('read-package-json')
20 // readJson(filename, [logFunction=noop], [strict=false], cb)
21 readJson('/path/to/package.json', console.error, false, function (er, data) {
23 console.error("There was an error reading the file")
27 console.error('the package data is', data)
31 ## readJson(file, [logFn = noop], [strict = false], cb)
33 * `file` {String} The path to the package.json file
34 * `logFn` {Function} Function to handle logging. Defaults to a noop.
35 * `strict` {Boolean} True to enforce SemVer 2.0 version strings, and
36 other strict requirements.
37 * `cb` {Function} Gets called with `(er, data)`, as is The Node Way.
39 Reads the JSON file and does the things.
41 ## `package.json` Fields
43 See `man 5 package.json` or `npm help json`.
47 By default this is a reference to the `npmlog` module. But if that
48 module can't be found, then it'll be set to just a dummy thing that does
51 Replace with your own `{log,warn,error}` object for fun loggy time.
53 ## readJson.extras(file, data, cb)
55 Run all the extra stuff relative to the file, with the parsed data.
57 Modifies the data as it does stuff. Calls the cb when it's done.
59 ## readJson.extraSet = [fn, fn, ...]
61 Array of functions that are called by `extras`. Each one receives the
62 arguments `fn(file, data, cb)` and is expected to call `cb(er, data)`
63 when done or when an error occurs.
65 Order is indeterminate, so each function should be completely
72 The `lru-cache` object that readJson uses to not read the same file over
74 [lru-cache](https://github.com/isaacs/node-lru-cache) for details.
76 ## Other Relevant Files Besides `package.json`
78 Some other files have an effect on the resulting data object, in the
83 If there is a `README` or `README.*` file present, then npm will attach
84 a `readme` field to the data with the contents of this file.
86 Owing to the fact that roughly 100% of existing node modules have
87 Markdown README files, it will generally be assumed to be Markdown,
88 regardless of the extension. Please plan accordingly.
92 If there is a `server.js` file, and there is not already a
93 `scripts.start` field, then `scripts.start` will be set to `node
98 If there is not already a `contributors` field, then the `contributors`
99 field will be set to the contents of the `AUTHORS` file, split by lines,
104 If a bindings.gyp file exists, and there is not already a
105 `scripts.install` field, then the `scripts.install` field will be set to
110 If the json file does not exist, but there is a `index.js` file
111 present instead, and that file has a package comment, then it will try
112 to parse the package comment, and use that as the data instead.
114 A package comment looks like this:
118 * { "name": "my-bare-module"
119 * , "version": "1.2.3"
120 * , "description": "etc...." }
126 { "name": "my-bare-module"
128 , "description": "etc...." }
132 The important thing is that it starts with `/**package`, and ends with
133 `**/`. If the package.json file exists, then the index.js is not
136 ### `{directories.man}/*.[0-9]`
138 If there is not already a `man` field defined as an array of files or a
140 there is a `directories.man` field defined, then that directory will
141 be searched for manpages.
143 Any valid manpages found in that directory will be assigned to the `man`
144 array, and installed in the appropriate man directory at package install
145 time, when installed globally on a Unix system.
147 ### `{directories.bin}/*`
149 If there is not already a `bin` field defined as a string filename or a
150 hash of `<name> : <filename>` pairs, then the `directories.bin`
151 directory will be searched and all the files within it will be linked as
152 executables at install time.
154 When installing locally, npm links bins into `node_modules/.bin`, which
155 is in the `PATH` environ when npm runs scripts. When
156 installing globally, they are linked into `{prefix}/bin`, which is
157 presumably in the `PATH` environment variable.