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.

init.md 4.9 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ---
  2. id: docs_cli_init
  3. guide: docs_cli
  4. layout: guide
  5. ---
  6. {% include vars.html %}
  7. <p class="lead">Interactively creates or updates a package.json file.</p>
  8. ##### `yarn init` <a class="toc" id="toc-yarn-init" href="#toc-yarn-init"></a>
  9. This command walks you through an interactive session to create a
  10. `package.json` file. Some defaults such as the license and initial version are
  11. found in yarn's `init-*` config settings.
  12. Here's an example of running the command inside of a directory named `testdir`:
  13. ```sh
  14. $ yarn init
  15. ```
  16. ```sh
  17. question name (testdir): my-awesome-package
  18. question version (1.0.0):
  19. question description: The best package you will ever find.
  20. question entry point (index.js):
  21. question git repository: https://github.com/yarnpkg/example-yarn-package
  22. question author: Yarn Contributor
  23. question license (MIT):
  24. question private:
  25. success Saved package.json
  26. ✨ Done in 87.70s.
  27. ```
  28. This results in the following `package.json`:
  29. ```json
  30. {
  31. "name": "my-awesome-package",
  32. "version": "1.0.0",
  33. "description": "The best package you will ever find.",
  34. "main": "index.js",
  35. "repository": {
  36. "url": "https://github.com/yarnpkg/example-yarn-package",
  37. "type": "git"
  38. },
  39. "author": "Yarn Contributor",
  40. "license": "MIT"
  41. }
  42. ```
  43. > By default, if answer given to `question private` is passed in as empty, the `private` key will not be added to `package.json`
  44. If you already have an existing `package.json` file, then it will use the
  45. file's entries as defaults.
  46. The following existing `package.json`:
  47. ```json
  48. {
  49. "name": "my-existing-package",
  50. "version": "0.1",
  51. "description": "I exist therefore I am.",
  52. "repository": {
  53. "url": "https://github.com/yarnpkg/example-yarn-package",
  54. "type": "git"
  55. },
  56. "license": "BSD-2-Clause"
  57. }
  58. ```
  59. Results in the following defaults during the interactive session:
  60. ```sh
  61. $ yarn init
  62. ```
  63. ```sh
  64. question name (my-existing-package):
  65. question version (0.1):
  66. question description (I exist therefore I am.):
  67. question entry point (index.js):
  68. question git repository (https://github.com/yarnpkg/example-yarn-package):
  69. question author: Yarn Contributor
  70. question license (BSD-2-Clause):
  71. question private:
  72. success Saved package.json
  73. ✨ Done in 121.53s.
  74. ```
  75. ##### Setting defaults for `yarn init` <a class="toc" id="toc-setting-defaults-for-yarn-init" href="#toc-setting-defaults-for-yarn-init"></a>
  76. The following [config]({{url_base}}/docs/cli/config) variables can be used to
  77. customize the defaults for `yarn init`:
  78. - `init-author-name`
  79. - `init-author-email`
  80. - `init-author-url`
  81. - `init-version`
  82. - `init-license`
  83. <!--- `init-private` waiting for https://github.com/yarnpkg/yarn/pull/4377 -->
  84. ##### `yarn init --yes/-y` <a class="toc" id="toc-yarn-init-yes-y" href="#toc-yarn-init-yes-y"></a>
  85. This command skips the interactive session mentioned above and generates a
  86. `package.json` based on your defaults. Some defaults may be modified changing
  87. `init-*` config settings like mentioned above. For example, given a fresh
  88. install of Yarn and inside a `yarn-example` directory:
  89. ```sh
  90. $ yarn init --yes
  91. ```
  92. ```
  93. warning The yes flag has been set. This will automatically answer yes to all questions which may have security implications.
  94. success Saved package.json
  95. ✨ Done in 0.09s.
  96. ```
  97. Which produces the following `package.json`:
  98. ```json
  99. {
  100. "name": "yarn-example",
  101. "version": "1.0.0",
  102. "main": "index.js",
  103. "license": "MIT"
  104. }
  105. ```
  106. ##### `yarn init --private/-p` <a class="toc" id="toc-yarn-init-private-p" href="#toc-yarn-init-private-p"></a>
  107. > automatically add `private: true` to the `package.json`
  108. ```sh
  109. $ yarn init --private
  110. ```
  111. If the `private` flag is set, the `private` key will be automatically set to `true` and you still complete the rest of the init process.
  112. ```sh
  113. question name (testdir): my-awesome-package
  114. question version (1.0.0):
  115. question description: The best package you will ever find.
  116. question entry point (index.js):
  117. question git repository: https://github.com/yarnpkg/example-yarn-package
  118. question author: Yarn Contributor
  119. question license (MIT):
  120. success Saved package.json
  121. ✨ Done in 87.70s.
  122. ```
  123. ```json
  124. {
  125. "name": "my-awesome-package",
  126. "version": "1.0.0",
  127. "description": "The best package you will ever find.",
  128. "main": "index.js",
  129. "repository": {
  130. "url": "https://github.com/yarnpkg/example-yarn-package",
  131. "type": "git"
  132. },
  133. "author": "Yarn Contributor",
  134. "license": "MIT",
  135. "private": true
  136. }
  137. ```
  138. **You can use both the `yes` and the `private` flags at the same time**
  139. Like this:
  140. ```sh
  141. $ yarn init -yp
  142. ```
  143. ```
  144. warning The yes flag has been set. This will automatically answer yes to all questions which may have security implications.
  145. success Saved package.json
  146. ✨ Done in 0.05s.
  147. ```
  148. Which produces the following `package.json`:
  149. ```json
  150. {
  151. "name": "yarn-example",
  152. "version": "1.0.0",
  153. "main": "index.js",
  154. "license": "MIT",
  155. "private": true
  156. }
  157. ```

js yarn包管理组件依赖分析

Contributors (1)