You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

package-json.md 13 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. ---
  2. id: docs_configuration_package_json
  3. guide: docs_configuration
  4. layout: guide
  5. ---
  6. {% include vars.html %}
  7. ## Essentials <a class="toc" id="toc-essentials" href="#toc-essentials"></a>
  8. The two most important fields in your `package.json` are `name` and `version`,
  9. without them your package won't be able to install. The `name` and `version`
  10. fields are used together to create a unique id.
  11. ### `name` <a class="toc" id="toc-name" href="#toc-name"></a>
  12. ```json
  13. {
  14. "name": "my-awesome-package"
  15. }
  16. ```
  17. This is the name of your package. It gets used in URLs, as an argument on the
  18. command line, and as the directory name inside `node_modules`.
  19. ```sh
  20. yarn add [name]
  21. ```
  22. ```
  23. node_modules/[name]
  24. ```
  25. ```
  26. https://registry.npmjs.org/[name]/-/[name]-[version].tgz
  27. ```
  28. **Rules**
  29. - Must be less than or equal to 214 characters (including the `@scope/` for
  30. scoped packages).
  31. - Must not start with a dot (`.`) or an underscore (`_`).
  32. - Must not have an uppercase letter in the name.
  33. - Must use only URL-safe characters.
  34. **Tips**
  35. - Don't use the same name as a core Node.js module
  36. - Don't put `js` or `node` in the name.
  37. - Keep names short and descriptive. You want people to understand what it is
  38. from the name, but it will also be used in `require()` calls.
  39. - Make sure that there isn't something in the
  40. [registry](https://www.npmjs.com/) with the same name.
  41. ### `version` <a class="toc" id="toc-version" href="#toc-version"></a>
  42. ```json
  43. {
  44. "version": "1.0.0"
  45. }
  46. ```
  47. The current version of your package.
  48. ## Info <a class="toc" id="toc-info" href="#toc-info"></a>
  49. ### `description` <a class="toc" id="toc-description" href="#toc-description"></a>
  50. ```json
  51. {
  52. "description": "My short description of my awesome package"
  53. }
  54. ```
  55. The description is just a string that helps people understand the purpose of the package. It can be used when searching for packages in a package manager as well.
  56. ### `keywords` <a class="toc" id="toc-keywords" href="#toc-keywords"></a>
  57. ```json
  58. {
  59. "keywords": ["short", "relevant", "keywords", "for", "searching"]
  60. }
  61. ```
  62. Keywords are an array of strings that are useful when searching for packages in a package manager.
  63. ### `license` <a class="toc" id="toc-license" href="#toc-license"></a>
  64. ```json
  65. {
  66. "license": "MIT",
  67. "license": "(MIT or GPL-3.0)",
  68. "license": "SEE LICENSE IN LICENSE_FILENAME.txt",
  69. "license": "UNLICENSED"
  70. }
  71. ```
  72. All packages should specify a license so that users know how they are permitted
  73. to use it and any restrictions that you are placing on it.
  74. You are encouraged to use an Open Source
  75. ([OSI-approved](https://opensource.org/licenses/alphabetical)) license unless
  76. you have a specific reason not to. If you built your package as part of your
  77. job it's likely best to check with your company before deciding on a license.
  78. **Must be one of the following:**
  79. - A valid [SPDX license identifier](https://spdx.org/licenses/) if you are
  80. using a standard license.
  81. - A valid
  82. [SPDX license expression syntax 2.0 expression](https://www.npmjs.com/package/spdx)
  83. if you are using multiple standard licenses.
  84. - A `SEE LICENSE IN <filename>` string that points to a `<filename>` in the top
  85. level of your package if you are using a non-standard license.
  86. - A `UNLICENSED` string if you do not want to grant others the right to use a
  87. private or unpublished package under any terms.
  88. ## Links <a class="toc" id="toc-links" href="#toc-links"></a>
  89. Various links to documentation, places to file issues and where your package code actually lives.
  90. ### `homepage` <a class="toc" id="toc-homepage" href="#toc-homepage"></a>
  91. ```json
  92. {
  93. "homepage": "https://your-package.org"
  94. }
  95. ```
  96. The homepage is the URL to the landing page or documentation for your package.
  97. ### `bugs` <a class="toc" id="toc-bugs" href="#toc-bugs"></a>
  98. ```json
  99. {
  100. "bugs": "https://github.com/user/repo/issues"
  101. }
  102. ```
  103. The URL to your project's issue tracker. This can also be something like an email address as well. It provides users a way to find out where to send issues with your package.
  104. ### `repository` <a class="toc" id="toc-repository" href="#toc-repository"></a>
  105. ```json
  106. {
  107. "repository": { "type": "git", "url": "https://github.com/user/repo.git" },
  108. "repository": "github:user/repo",
  109. "repository": "gitlab:user/repo",
  110. "repository": "bitbucket:user/repo",
  111. "repository": "gist:a1b2c3d4e5f"
  112. }
  113. ```
  114. The repository is the location where the actual code for your package lives.
  115. ## Maintainers <a class="toc" id="toc-maintainers" href="#toc-maintainers"></a>
  116. The maintainers of your project.
  117. ### `author` <a class="toc" id="toc-author" href="#toc-author"></a>
  118. ```json
  119. {
  120. "author": {
  121. "name": "Your Name",
  122. "email": "you@example.com",
  123. "url": "http://your-website.com"
  124. },
  125. "author": "Your Name <you@example.com> (http://your-website.com)"
  126. }
  127. ```
  128. Package author information. An author is one person.
  129. ### `contributors` <a class="toc" id="toc-contributors" href="#toc-contributors"></a>
  130. ```json
  131. {
  132. "contributors": [
  133. { "name": "Your Friend", "email": "friend@example.com", "url": "http://friends-website.com" }
  134. { "name": "Other Friend", "email": "other@example.com", "url": "http://other-website.com" }
  135. ],
  136. "contributors": [
  137. "Your Friend <friend@example.com> (http://friends-website.com)",
  138. "Other Friend <other@example.com> (http://other-website.com)"
  139. ]
  140. }
  141. ```
  142. Those that have contributed to your package. Contributors are an array of people.
  143. ## Files <a class="toc" id="toc-files" href="#toc-files"></a>
  144. You can specify files that will be included in your project, along with the main entry point for your project.
  145. ### `files` <a class="toc" id="toc-files" href="#toc-files"></a>
  146. ```json
  147. {
  148. "files": ["filename.js", "directory/", "glob/*.{js,json}"]
  149. }
  150. ```
  151. These are files that are included in your project. You can specify single files, whole directories or use wildcards to include files that meet a certain criteria.
  152. ### `main` <a class="toc" id="toc-main" href="#toc-main"></a>
  153. ```json
  154. {
  155. "main": "filename.js"
  156. }
  157. ```
  158. This is the primary entry point for the functionality for your project.
  159. ### `bin` <a class="toc" id="toc-bin" href="#toc-bin"></a>
  160. ```json
  161. {
  162. "bin": "bin.js",
  163. "bin": {
  164. "command-name": "bin/command-name.js",
  165. "other-command": "bin/other-command"
  166. }
  167. }
  168. ```
  169. Executable files included with your project that will be installed.
  170. ### `man` <a class="toc" id="toc-man" href="#toc-man"></a>
  171. ```json
  172. {
  173. "man": "./man/doc.1",
  174. "man": ["./man/doc.1", "./man/doc.2"]
  175. }
  176. ```
  177. If you have man pages associated with your project, add them here.
  178. ### `directories` <a class="toc" id="toc-directories" href="#toc-directories"></a>
  179. ```json
  180. {
  181. "directories": {
  182. "lib": "path/to/lib/",
  183. "bin": "path/to/bin/",
  184. "man": "path/to/man/",
  185. "doc": "path/to/doc/",
  186. "example": "path/to/example/"
  187. }
  188. }
  189. ```
  190. When installing your package, you can specify exact locations to put binary files, man pages, documentation, examples, etc.
  191. ## Tasks <a class="toc" id="toc-tasks" href="#toc-tasks"></a>
  192. Your package can include runnable scripts or other configuration.
  193. ### `scripts` <a class="toc" id="toc-scripts" href="#toc-scripts"></a>
  194. ```json
  195. {
  196. "scripts": {
  197. "build-project": "node build-project.js"
  198. }
  199. }
  200. ```
  201. Scripts are a great way of automating tasks related to your package, such as simple build processes or development tools. Using the `"scripts"` field, you can define various scripts to be run as `yarn run <script>`. For example, the `build-project` script above can be invoked with `yarn run build-project` and will run `node build-project.js`.
  202. Certain script names are special. If defined, the `preinstall` script is called by yarn before your package is installed. For compatibility reasons, scripts called `install`, `postinstall`, `prepublish`, and `prepare` will all be called after your package has finished installing.
  203. The `start` script value defaults to `node server.js`.
  204. ### `config` <a class="toc" id="toc-config" href="#toc-config"></a>
  205. ```json
  206. {
  207. "config": {
  208. "port": "8080"
  209. }
  210. }
  211. ```
  212. Configuration options or parameters used in your scripts.
  213. ## Dependencies <a class="toc" id="toc-dependencies" href="#toc-dependencies"></a>
  214. Your package will very likely depend on other packages. You can specify those dependencies in your `package.json` file.
  215. ### `dependencies` <a class="toc" id="toc-dependencies" href="#toc-dependencies"></a>
  216. ```json
  217. {
  218. "dependencies": {
  219. "package-1": "^3.1.4"
  220. }
  221. }
  222. ```
  223. These are dependencies that are required in both development and production for your package.
  224. > You can specify an exact version, a minimum version (e.g., `>=`) or a range of versions (e.g. `>= ... <`).
  225. ### `devDependencies` <a class="toc" id="toc-devdependencies" href="#toc-devdependencies"></a>
  226. ```json
  227. {
  228. "devDependencies": {
  229. "package-2": "^0.4.2"
  230. }
  231. }
  232. ```
  233. These are packages that are only required when developing your package but will not be installed in production.
  234. ### `peerDependencies` <a class="toc" id="toc-peerdependencies" href="#toc-peerdependencies"></a>
  235. ```json
  236. {
  237. "peerDependencies": {
  238. "package-3": "^2.7.18"
  239. }
  240. }
  241. ```
  242. Peer dependencies allow you to state compatibility of your package with versions of other packages.
  243. ### `peerDependenciesMeta` <a class="toc" id="toc-peerdependenciesmeta" href="#toc-peerdependenciesmeta"></a>
  244. ```json
  245. {
  246. "peerDependenciesMeta": {
  247. "package-3": {
  248. "optional": true
  249. }
  250. }
  251. }
  252. ```
  253. Allows you to add metadata to peer dependencies.
  254. Currently only the `optional` tag is available. Setting it to true will suppress the warning for a missing peer dependency.
  255. ### `optionalDependencies` <a class="toc" id="toc-optionaldependencies" href="#toc-optionaldependencies"></a>
  256. ```json
  257. {
  258. "optionalDependencies": {
  259. "package-5": "^1.6.1"
  260. }
  261. }
  262. ```
  263. Optional dependencies can be used with your package, but are not required. If the optional package is not found, installation still continues.
  264. ### `bundledDependencies` <a class="toc" id="toc-bundleddependencies" href="#toc-bundleddependencies"></a>
  265. ```json
  266. {
  267. "bundledDependencies": ["package-4"]
  268. }
  269. ```
  270. Bundled dependencies are an array of package names that will be bundled together when publishing your package.
  271. ### `flat` <a class="toc" id="toc-flat" href="#toc-flat"></a>
  272. ```json
  273. {
  274. "flat": true
  275. }
  276. ```
  277. If your package only allows one version of a given dependency, and you'd like to enforce the same behavior as [`yarn install --flat`]({{url_base}}/docs/cli/install#toc-yarn-install-flat) on the command line, set this to `true`.
  278. Note that if your `package.json` contains `"flat": true` and other packages depend on yours (e.g. you are building a library rather than an application), those other packages will also need `"flat": true` in their `package.json` or be installed with `yarn install --flat` on the command line.
  279. ### `resolutions` <a class="toc" id="toc-resolutions" href="#toc-resolutions"></a>
  280. ```json
  281. {
  282. "resolutions": {
  283. "transitive-package-1": "0.0.29",
  284. "transitive-package-2": "file:./local-forks/transitive-package-2",
  285. "dependencies-package-1/transitive-package-3": "^2.1.1"
  286. }
  287. }
  288. ```
  289. Allows you to override a version of a particular nested dependency. See [the Selective Versions Resolutions RFC](https://github.com/yarnpkg/rfcs/blob/master/implemented/0000-selective-versions-resolutions.md) for the full spec.
  290. Note that installing dependencies via [`yarn install --flat`]({{url_base}}/docs/cli/install#toc-yarn-install-flat) will automatically add a `resolutions` block to your `package.json` file.
  291. ## System <a class="toc" id="toc-system" href="#toc-system"></a>
  292. You can provide system-level information associated with your package, such as operating system compatibility, etc.
  293. ### `engines` <a class="toc" id="toc-engines" href="#toc-engines"></a>
  294. ```json
  295. {
  296. "engines": {
  297. "node": ">=4.4.7 <7.0.0",
  298. "zlib": "^1.2.8",
  299. "yarn": "^0.14.0"
  300. }
  301. }
  302. ```
  303. The engines specify versions of clients that must be used with your package. This checks against `process.versions` as well as the current version of yarn.
  304. This check follows normal semver rules with one exception. It allows prerelease versions to match semvers that do not explicitly specify a prerelease. For example, `1.4.0-rc.0` matches `>=1.3.0`, while it would not match a typical semver check.
  305. ### `os` <a class="toc" id="toc-os" href="#toc-os"></a>
  306. ```json
  307. {
  308. "os": ["darwin", "linux"],
  309. "os": ["!win32"]
  310. }
  311. ```
  312. This specifies operating system compatibility for your package. It checks against `process.platform`.
  313. ### `cpu` <a class="toc" id="toc-cpu" href="#toc-cpu"></a>
  314. ```json
  315. {
  316. "cpu": ["x64", "ia32"],
  317. "cpu": ["!arm", "!mips"]
  318. }
  319. ```
  320. Use this to specify your package will only run on certain CPU architectures. This checks against `process.arch`.
  321. ## Publishing <a class="toc" id="toc-publishing" href="#toc-publishing"></a>
  322. ### `private` <a class="toc" id="toc-private" href="#toc-private"></a>
  323. ```json
  324. {
  325. "private": true
  326. }
  327. ```
  328. If you do not want your package published in a package manager, set this to `true`.
  329. ### `publishConfig` <a class="toc" id="toc-publishconfig" href="#toc-publishconfig"></a>
  330. ```json
  331. {
  332. "publishConfig": {
  333. ...
  334. }
  335. }
  336. ```
  337. These configuration values will be used when publishing your package. You can tag your package, for example.

js yarn包管理组件依赖分析

Contributors (1)