npm-init.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <html><head>
  2. <title>npm-init</title>
  3. <style>
  4. body {
  5. background-color: #ffffff;
  6. color: #24292e;
  7. margin: 0;
  8. line-height: 1.5;
  9. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  10. }
  11. #rainbar {
  12. height: 10px;
  13. background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
  14. }
  15. a {
  16. text-decoration: none;
  17. color: #0366d6;
  18. }
  19. a:hover {
  20. text-decoration: underline;
  21. }
  22. pre {
  23. margin: 1em 0px;
  24. padding: 1em;
  25. border: solid 1px #e1e4e8;
  26. border-radius: 6px;
  27. display: block;
  28. overflow: auto;
  29. white-space: pre;
  30. background-color: #f6f8fa;
  31. color: #393a34;
  32. }
  33. code {
  34. font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
  35. font-size: 85%;
  36. padding: 0.2em 0.4em;
  37. background-color: #f6f8fa;
  38. color: #393a34;
  39. }
  40. pre > code {
  41. padding: 0;
  42. background-color: inherit;
  43. color: inherit;
  44. }
  45. h1, h2, h3 {
  46. font-weight: 600;
  47. }
  48. #logobar {
  49. background-color: #333333;
  50. margin: 0 auto;
  51. padding: 1em 4em;
  52. }
  53. #logobar .logo {
  54. float: left;
  55. }
  56. #logobar .title {
  57. font-weight: 600;
  58. color: #dddddd;
  59. float: left;
  60. margin: 5px 0 0 1em;
  61. }
  62. #logobar:after {
  63. content: "";
  64. display: block;
  65. clear: both;
  66. }
  67. #content {
  68. margin: 0 auto;
  69. padding: 0 4em;
  70. }
  71. #table_of_contents > h2 {
  72. font-size: 1.17em;
  73. }
  74. #table_of_contents ul:first-child {
  75. border: solid 1px #e1e4e8;
  76. border-radius: 6px;
  77. padding: 1em;
  78. background-color: #f6f8fa;
  79. color: #393a34;
  80. }
  81. #table_of_contents ul {
  82. list-style-type: none;
  83. padding-left: 1.5em;
  84. }
  85. #table_of_contents li {
  86. font-size: 0.9em;
  87. }
  88. #table_of_contents li a {
  89. color: #000000;
  90. }
  91. header.title {
  92. border-bottom: solid 1px #e1e4e8;
  93. }
  94. header.title > h1 {
  95. margin-bottom: 0.25em;
  96. }
  97. header.title > .description {
  98. display: block;
  99. margin-bottom: 0.5em;
  100. line-height: 1;
  101. }
  102. footer#edit {
  103. border-top: solid 1px #e1e4e8;
  104. margin: 3em 0 4em 0;
  105. padding-top: 2em;
  106. }
  107. </style>
  108. </head>
  109. <body>
  110. <div id="banner">
  111. <div id="rainbar"></div>
  112. <div id="logobar">
  113. <svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
  114. <polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
  115. <polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
  116. </svg>
  117. <div class="title">
  118. npm command-line interface
  119. </div>
  120. </div>
  121. </div>
  122. <section id="content">
  123. <header class="title">
  124. <h1 id="npm-init">npm-init</h1>
  125. <span class="description">Create a package.json file</span>
  126. </header>
  127. <section id="table_of_contents">
  128. <h2 id="table-of-contents">Table of contents</h2>
  129. <div id="_table_of_contents"><ul><li><a href="#synopsis">Synopsis</a></li><li><a href="#description">Description</a></li><ul><li><a href="#forwarding-additional-options">Forwarding additional options</a></li></ul><li><a href="#examples">Examples</a></li><li><a href="#workspaces-support">Workspaces support</a></li><li><a href="#configuration">Configuration</a></li><ul><li><a href="#yes"><code>yes</code></a></li><li><a href="#force"><code>force</code></a></li><li><a href="#workspace"><code>workspace</code></a></li><li><a href="#workspaces"><code>workspaces</code></a></li><li><a href="#include-workspace-root"><code>include-workspace-root</code></a></li></ul><li><a href="#see-also">See Also</a></li></ul></div>
  130. </section>
  131. <div id="_content"><h3 id="synopsis">Synopsis</h3>
  132. <pre lang="bash"><code>npm init [--yes|-y|--scope]
  133. npm init &lt;@scope&gt; (same as `npm exec &lt;@scope&gt;/create`)
  134. npm init [&lt;@scope&gt;/]&lt;name&gt; (same as `npm exec [&lt;@scope&gt;/]create-&lt;name&gt;`)
  135. npm init [-w &lt;dir&gt;] [args...]
  136. </code></pre>
  137. <h3 id="description">Description</h3>
  138. <p><code>npm init &lt;initializer&gt;</code> can be used to set up a new or existing npm
  139. package.</p>
  140. <p><code>initializer</code> in this case is an npm package named <code>create-&lt;initializer&gt;</code>,
  141. which will be installed by <a href="../commands/npm-exec.html"><code>npm-exec</code></a>, and then have its
  142. main bin executed – presumably creating or updating <code>package.json</code> and
  143. running any other initialization-related operations.</p>
  144. <p>The init command is transformed to a corresponding <code>npm exec</code> operation as
  145. follows:</p>
  146. <ul>
  147. <li><code>npm init foo</code> -&gt; <code>npm exec create-foo</code></li>
  148. <li><code>npm init @usr/foo</code> -&gt; <code>npm exec @usr/create-foo</code></li>
  149. <li><code>npm init @usr</code> -&gt; <code>npm exec @usr/create</code></li>
  150. </ul>
  151. <p>If the initializer is omitted (by just calling <code>npm init</code>), init will fall
  152. back to legacy init behavior. It will ask you a bunch of questions, and
  153. then write a package.json for you. It will attempt to make reasonable
  154. guesses based on existing fields, dependencies, and options selected. It is
  155. strictly additive, so it will keep any fields and values that were already
  156. set. You can also use <code>-y</code>/<code>--yes</code> to skip the questionnaire altogether. If
  157. you pass <code>--scope</code>, it will create a scoped package.</p>
  158. <h4 id="forwarding-additional-options">Forwarding additional options</h4>
  159. <p>Any additional options will be passed directly to the command, so <code>npm init foo -- --hello</code> will map to <code>npm exec -- create-foo --hello</code>.</p>
  160. <p>To better illustrate how options are forwarded, here’s a more evolved
  161. example showing options passed to both the <strong>npm cli</strong> and a create package,
  162. both following commands are equivalent:</p>
  163. <ul>
  164. <li><code>npm init foo -y --registry=&lt;url&gt; -- --hello -a</code></li>
  165. <li><code>npm exec -y --registry=&lt;url&gt; -- create-foo --hello -a</code></li>
  166. </ul>
  167. <h3 id="examples">Examples</h3>
  168. <p>Create a new React-based project using
  169. <a href="https://npm.im/create-react-app"><code>create-react-app</code></a>:</p>
  170. <pre lang="bash"><code>$ npm init react-app ./my-react-app
  171. </code></pre>
  172. <p>Create a new <code>esm</code>-compatible package using
  173. <a href="https://npm.im/create-esm"><code>create-esm</code></a>:</p>
  174. <pre lang="bash"><code>$ mkdir my-esm-lib &amp;&amp; cd my-esm-lib
  175. $ npm init esm --yes
  176. </code></pre>
  177. <p>Generate a plain old package.json using legacy init:</p>
  178. <pre lang="bash"><code>$ mkdir my-npm-pkg &amp;&amp; cd my-npm-pkg
  179. $ git init
  180. $ npm init
  181. </code></pre>
  182. <p>Generate it without having it ask any questions:</p>
  183. <pre lang="bash"><code>$ npm init -y
  184. </code></pre>
  185. <h3 id="workspaces-support">Workspaces support</h3>
  186. <p>It’s possible to create a new workspace within your project by using the
  187. <code>workspace</code> config option. When using <code>npm init -w &lt;dir&gt;</code> the cli will
  188. create the folders and boilerplate expected while also adding a reference
  189. to your project <code>package.json</code> <code>"workspaces": []</code> property in order to make
  190. sure that new generated <strong>workspace</strong> is properly set up as such.</p>
  191. <p>Given a project with no workspaces, e.g:</p>
  192. <pre><code>.
  193. +-- package.json
  194. </code></pre>
  195. <p>You may generate a new workspace using the legacy init:</p>
  196. <pre lang="bash"><code>$ npm init -w packages/a
  197. </code></pre>
  198. <p>That will generate a new folder and <code>package.json</code> file, while also updating
  199. your top-level <code>package.json</code> to add the reference to this new workspace:</p>
  200. <pre><code>.
  201. +-- package.json
  202. `-- packages
  203. `-- a
  204. `-- package.json
  205. </code></pre>
  206. <p>The workspaces init also supports the <code>npm init &lt;initializer&gt; -w &lt;dir&gt;</code>
  207. syntax, following the same set of rules explained earlier in the initial
  208. <strong>Description</strong> section of this page. Similar to the previous example of
  209. creating a new React-based project using
  210. <a href="https://npm.im/create-react-app"><code>create-react-app</code></a>, the following syntax
  211. will make sure to create the new react app as a nested <strong>workspace</strong> within your
  212. project and configure your <code>package.json</code> to recognize it as such:</p>
  213. <pre lang="bash"><code>npm init -w packages/my-react-app react-app .
  214. </code></pre>
  215. <p>This will make sure to generate your react app as expected, one important
  216. consideration to have in mind is that <code>npm exec</code> is going to be run in the
  217. context of the newly created folder for that workspace, and that’s the reason
  218. why in this example the initializer uses the initializer name followed with a
  219. dot to represent the current directory in that context, e.g: <code>react-app .</code>:</p>
  220. <pre><code>.
  221. +-- package.json
  222. `-- packages
  223. +-- a
  224. | `-- package.json
  225. `-- my-react-app
  226. +-- README
  227. +-- package.json
  228. `-- ...
  229. </code></pre>
  230. <h3 id="configuration">Configuration</h3>
  231. <!-- raw HTML omitted -->
  232. <!-- raw HTML omitted -->
  233. <!-- raw HTML omitted -->
  234. <h4 id="yes"><code>yes</code></h4>
  235. <ul>
  236. <li>Default: null</li>
  237. <li>Type: null or Boolean</li>
  238. </ul>
  239. <p>Automatically answer “yes” to any prompts that npm might print on the
  240. command line.</p>
  241. <!-- raw HTML omitted -->
  242. <!-- raw HTML omitted -->
  243. <h4 id="force"><code>force</code></h4>
  244. <ul>
  245. <li>Default: false</li>
  246. <li>Type: Boolean</li>
  247. </ul>
  248. <p>Removes various protections against unfortunate side effects, common
  249. mistakes, unnecessary performance degradation, and malicious input.</p>
  250. <ul>
  251. <li>Allow clobbering non-npm files in global installs.</li>
  252. <li>Allow the <code>npm version</code> command to work on an unclean git repository.</li>
  253. <li>Allow deleting the cache folder with <code>npm cache clean</code>.</li>
  254. <li>Allow installing packages that have an <code>engines</code> declaration requiring a
  255. different version of npm.</li>
  256. <li>Allow installing packages that have an <code>engines</code> declaration requiring a
  257. different version of <code>node</code>, even if <code>--engine-strict</code> is enabled.</li>
  258. <li>Allow <code>npm audit fix</code> to install modules outside your stated dependency
  259. range (including SemVer-major changes).</li>
  260. <li>Allow unpublishing all versions of a published package.</li>
  261. <li>Allow conflicting peerDependencies to be installed in the root project.</li>
  262. <li>Implicitly set <code>--yes</code> during <code>npm init</code>.</li>
  263. <li>Allow clobbering existing values in <code>npm pkg</code></li>
  264. </ul>
  265. <p>If you don’t have a clear idea of what you want to do, it is strongly
  266. recommended that you do not use this option!</p>
  267. <!-- raw HTML omitted -->
  268. <!-- raw HTML omitted -->
  269. <h4 id="workspace"><code>workspace</code></h4>
  270. <ul>
  271. <li>Default:</li>
  272. <li>Type: String (can be set multiple times)</li>
  273. </ul>
  274. <p>Enable running a command in the context of the configured workspaces of the
  275. current project while filtering by running only the workspaces defined by
  276. this configuration option.</p>
  277. <p>Valid values for the <code>workspace</code> config are either:</p>
  278. <ul>
  279. <li>Workspace names</li>
  280. <li>Path to a workspace directory</li>
  281. <li>Path to a parent workspace directory (will result in selecting all
  282. workspaces within that folder)</li>
  283. </ul>
  284. <p>When set for the <code>npm init</code> command, this may be set to the folder of a
  285. workspace which does not yet exist, to create the folder and set it up as a
  286. brand new workspace within the project.</p>
  287. <p>This value is not exported to the environment for child processes.</p>
  288. <!-- raw HTML omitted -->
  289. <!-- raw HTML omitted -->
  290. <h4 id="workspaces"><code>workspaces</code></h4>
  291. <ul>
  292. <li>Default: null</li>
  293. <li>Type: null or Boolean</li>
  294. </ul>
  295. <p>Set to true to run the command in the context of <strong>all</strong> configured
  296. workspaces.</p>
  297. <p>Explicitly setting this to false will cause commands like <code>install</code> to
  298. ignore workspaces altogether. When not set explicitly:</p>
  299. <ul>
  300. <li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
  301. will link workspaces into the <code>node_modules</code> folder. - Commands that do
  302. other things (test, exec, publish, etc.) will operate on the root project,
  303. <em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li>
  304. </ul>
  305. <p>This value is not exported to the environment for child processes.</p>
  306. <!-- raw HTML omitted -->
  307. <!-- raw HTML omitted -->
  308. <h4 id="include-workspace-root"><code>include-workspace-root</code></h4>
  309. <ul>
  310. <li>Default: false</li>
  311. <li>Type: Boolean</li>
  312. </ul>
  313. <p>Include the workspace root when workspaces are enabled for a command.</p>
  314. <p>When false, specifying individual workspaces via the <code>workspace</code> config, or
  315. all workspaces via the <code>workspaces</code> flag, will cause npm to operate only on
  316. the specified workspaces, and not on the root project.</p>
  317. <!-- raw HTML omitted -->
  318. <!-- raw HTML omitted -->
  319. <!-- raw HTML omitted -->
  320. <h3 id="see-also">See Also</h3>
  321. <ul>
  322. <li><a href="http://npm.im/init-package-json">init-package-json module</a></li>
  323. <li><a href="../configuring-npm/package-json.html">package.json</a></li>
  324. <li><a href="../commands/npm-version.html">npm version</a></li>
  325. <li><a href="../using-npm/scope.html">npm scope</a></li>
  326. <li><a href="../commands/npm-exec.html">npm exec</a></li>
  327. <li><a href="../using-npm/workspaces.html">npm workspaces</a></li>
  328. </ul>
  329. </div>
  330. <footer id="edit">
  331. <a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-init.md">
  332. <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
  333. <path fill-rule="evenodd" d="M11.013 1.427a1.75 1.75 0 012.474 0l1.086 1.086a1.75 1.75 0 010 2.474l-8.61 8.61c-.21.21-.47.364-.756.445l-3.251.93a.75.75 0 01-.927-.928l.929-3.25a1.75 1.75 0 01.445-.758l8.61-8.61zm1.414 1.06a.25.25 0 00-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 000-.354l-1.086-1.086zM11.189 6.25L9.75 4.81l-6.286 6.287a.25.25 0 00-.064.108l-.558 1.953 1.953-.558a.249.249 0 00.108-.064l6.286-6.286z"></path>
  334. </svg>
  335. Edit this page on GitHub
  336. </a>
  337. </footer>
  338. </section>
  339. </body></html>