• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html><html><head>
2<meta charset="utf-8">
3<title>npm-init</title>
4<style>
5body {
6    background-color: #ffffff;
7    color: #24292e;
8
9    margin: 0;
10
11    line-height: 1.5;
12
13    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
14}
15#rainbar {
16    height: 10px;
17    background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
18}
19
20a {
21    text-decoration: none;
22    color: #0366d6;
23}
24a:hover {
25    text-decoration: underline;
26}
27
28pre {
29    margin: 1em 0px;
30    padding: 1em;
31    border: solid 1px #e1e4e8;
32    border-radius: 6px;
33
34    display: block;
35    overflow: auto;
36
37    white-space: pre;
38
39    background-color: #f6f8fa;
40    color: #393a34;
41}
42code {
43    font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
44    font-size: 85%;
45    padding: 0.2em 0.4em;
46    background-color: #f6f8fa;
47    color: #393a34;
48}
49pre > code {
50    padding: 0;
51    background-color: inherit;
52    color: inherit;
53}
54h1, h2, h3 {
55    font-weight: 600;
56}
57
58#logobar {
59    background-color: #333333;
60    margin: 0 auto;
61    padding: 1em 4em;
62}
63#logobar .logo {
64    float: left;
65}
66#logobar .title {
67    font-weight: 600;
68    color: #dddddd;
69    float: left;
70    margin: 5px 0 0 1em;
71}
72#logobar:after {
73    content: "";
74    display: block;
75    clear: both;
76}
77
78#content {
79    margin: 0 auto;
80    padding: 0 4em;
81}
82
83#table_of_contents > h2 {
84    font-size: 1.17em;
85}
86#table_of_contents ul:first-child {
87    border: solid 1px #e1e4e8;
88    border-radius: 6px;
89    padding: 1em;
90    background-color: #f6f8fa;
91    color: #393a34;
92}
93#table_of_contents ul {
94    list-style-type: none;
95    padding-left: 1.5em;
96}
97#table_of_contents li {
98    font-size: 0.9em;
99}
100#table_of_contents li a {
101    color: #000000;
102}
103
104header.title {
105    border-bottom: solid 1px #e1e4e8;
106}
107header.title > h1 {
108    margin-bottom: 0.25em;
109}
110header.title > .description {
111    display: block;
112    margin-bottom: 0.5em;
113    line-height: 1;
114}
115
116footer#edit {
117    border-top: solid 1px #e1e4e8;
118    margin: 3em 0 4em 0;
119    padding-top: 2em;
120}
121</style>
122</head>
123<body>
124<div id="banner">
125<div id="rainbar"></div>
126<div id="logobar">
127<svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
128<polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
129<polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
130</svg>
131<div class="title">
132npm command-line interface
133</div>
134</div>
135</div>
136
137<section id="content">
138<header class="title">
139<h1 id="npm-init">npm-init</h1>
140<span class="description">Create a package.json file</span>
141</header>
142
143<section id="table_of_contents">
144<h2 id="table-of-contents">Table of contents</h2>
145<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="#init-author-name"><code>init-author-name</code></a></li><li><a href="#init-author-url"><code>init-author-url</code></a></li><li><a href="#init-license"><code>init-license</code></a></li><li><a href="#init-module"><code>init-module</code></a></li><li><a href="#init-version"><code>init-version</code></a></li><li><a href="#yes"><code>yes</code></a></li><li><a href="#force"><code>force</code></a></li><li><a href="#scope"><code>scope</code></a></li><li><a href="#workspace"><code>workspace</code></a></li><li><a href="#workspaces"><code>workspaces</code></a></li><li><a href="#workspaces-update"><code>workspaces-update</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>
146</section>
147
148<div id="_content"><h3 id="synopsis">Synopsis</h3>
149<pre><code class="language-bash">npm init &lt;package-spec&gt; (same as `npx &lt;package-spec&gt;`)
150npm init &lt;@scope&gt; (same as `npx &lt;@scope&gt;/create`)
151
152aliases: create, innit
153</code></pre>
154<h3 id="description">Description</h3>
155<p><code>npm init &lt;initializer&gt;</code> can be used to set up a new or existing npm
156package.</p>
157<p><code>initializer</code> in this case is an npm package named <code>create-&lt;initializer&gt;</code>,
158which will be installed by <a href="../commands/npm-exec.html"><code>npm-exec</code></a>, and then have its
159main bin executed -- presumably creating or updating <code>package.json</code> and
160running any other initialization-related operations.</p>
161<p>The init command is transformed to a corresponding <code>npm exec</code> operation as
162follows:</p>
163<ul>
164<li><code>npm init foo</code> -&gt; <code>npm exec create-foo</code></li>
165<li><code>npm init @usr/foo</code> -&gt; <code>npm exec @usr/create-foo</code></li>
166<li><code>npm init @usr</code> -&gt; <code>npm exec @usr/create</code></li>
167<li><code>npm init @usr@2.0.0</code> -&gt; <code>npm exec @usr/create@2.0.0</code></li>
168<li><code>npm init @usr/foo@2.0.0</code> -&gt; <code>npm exec @usr/create-foo@2.0.0</code></li>
169</ul>
170<p>If the initializer is omitted (by just calling <code>npm init</code>), init will fall
171back to legacy init behavior. It will ask you a bunch of questions, and
172then write a package.json for you. It will attempt to make reasonable
173guesses based on existing fields, dependencies, and options selected. It is
174strictly additive, so it will keep any fields and values that were already
175set. You can also use <code>-y</code>/<code>--yes</code> to skip the questionnaire altogether. If
176you pass <code>--scope</code>, it will create a scoped package.</p>
177<p><em>Note:</em> if a user already has the <code>create-&lt;initializer&gt;</code> package
178globally installed, that will be what <code>npm init</code> uses.  If you want npm
179to use the latest version, or another specific version you must specify
180it:</p>
181<ul>
182<li><code>npm init foo@latest</code> # fetches and runs the latest <code>create-foo</code> from
183the registry</li>
184<li><code>npm init foo@1.2.3</code> #  runs <code>create-foo@1.2.3</code> specifically</li>
185</ul>
186<h4 id="forwarding-additional-options">Forwarding additional options</h4>
187<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>
188<p>To better illustrate how options are forwarded, here's a more evolved
189example showing options passed to both the <strong>npm cli</strong> and a create package,
190both following commands are equivalent:</p>
191<ul>
192<li><code>npm init foo -y --registry=&lt;url&gt; -- --hello -a</code></li>
193<li><code>npm exec -y --registry=&lt;url&gt; -- create-foo --hello -a</code></li>
194</ul>
195<h3 id="examples">Examples</h3>
196<p>Create a new React-based project using
197<a href="https://npm.im/create-react-app"><code>create-react-app</code></a>:</p>
198<pre><code class="language-bash">$ npm init react-app ./my-react-app
199</code></pre>
200<p>Create a new <code>esm</code>-compatible package using
201<a href="https://npm.im/create-esm"><code>create-esm</code></a>:</p>
202<pre><code class="language-bash">$ mkdir my-esm-lib &amp;&amp; cd my-esm-lib
203$ npm init esm --yes
204</code></pre>
205<p>Generate a plain old package.json using legacy init:</p>
206<pre><code class="language-bash">$ mkdir my-npm-pkg &amp;&amp; cd my-npm-pkg
207$ git init
208$ npm init
209</code></pre>
210<p>Generate it without having it ask any questions:</p>
211<pre><code class="language-bash">$ npm init -y
212</code></pre>
213<h3 id="workspaces-support">Workspaces support</h3>
214<p>It's possible to create a new workspace within your project by using the
215<code>workspace</code> config option. When using <code>npm init -w &lt;dir&gt;</code> the cli will
216create the folders and boilerplate expected while also adding a reference
217to your project <code>package.json</code> <code>"workspaces": []</code> property in order to make
218sure that new generated <strong>workspace</strong> is properly set up as such.</p>
219<p>Given a project with no workspaces, e.g:</p>
220<pre><code>.
221+-- package.json
222</code></pre>
223<p>You may generate a new workspace using the legacy init:</p>
224<pre><code class="language-bash">$ npm init -w packages/a
225</code></pre>
226<p>That will generate a new folder and <code>package.json</code> file, while also updating
227your top-level <code>package.json</code> to add the reference to this new workspace:</p>
228<pre><code>.
229+-- package.json
230`-- packages
231   `-- a
232       `-- package.json
233</code></pre>
234<p>The workspaces init also supports the <code>npm init &lt;initializer&gt; -w &lt;dir&gt;</code>
235syntax, following the same set of rules explained earlier in the initial
236<strong>Description</strong> section of this page. Similar to the previous example of
237creating a new React-based project using
238<a href="https://npm.im/create-react-app"><code>create-react-app</code></a>, the following syntax
239will make sure to create the new react app as a nested <strong>workspace</strong> within your
240project and configure your <code>package.json</code> to recognize it as such:</p>
241<pre><code class="language-bash">npm init -w packages/my-react-app react-app .
242</code></pre>
243<p>This will make sure to generate your react app as expected, one important
244consideration to have in mind is that <code>npm exec</code> is going to be run in the
245context of the newly created folder for that workspace, and that's the reason
246why in this example the initializer uses the initializer name followed with a
247dot to represent the current directory in that context, e.g: <code>react-app .</code>:</p>
248<pre><code>.
249+-- package.json
250`-- packages
251   +-- a
252   |   `-- package.json
253   `-- my-react-app
254       +-- README
255       +-- package.json
256       `-- ...
257</code></pre>
258<h3 id="configuration">Configuration</h3>
259<h4 id="init-author-name"><code>init-author-name</code></h4>
260<ul>
261<li>Default: ""</li>
262<li>Type: String</li>
263</ul>
264<p>The value <code>npm init</code> should use by default for the package author's name.</p>
265<h4 id="init-author-url"><code>init-author-url</code></h4>
266<ul>
267<li>Default: ""</li>
268<li>Type: "" or URL</li>
269</ul>
270<p>The value <code>npm init</code> should use by default for the package author's
271homepage.</p>
272<h4 id="init-license"><code>init-license</code></h4>
273<ul>
274<li>Default: "ISC"</li>
275<li>Type: String</li>
276</ul>
277<p>The value <code>npm init</code> should use by default for the package license.</p>
278<h4 id="init-module"><code>init-module</code></h4>
279<ul>
280<li>Default: "~/.npm-init.js"</li>
281<li>Type: Path</li>
282</ul>
283<p>A module that will be loaded by the <code>npm init</code> command. See the
284documentation for the
285<a href="https://github.com/npm/init-package-json">init-package-json</a> module for
286more information, or <a href="../commands/npm-init.html">npm init</a>.</p>
287<h4 id="init-version"><code>init-version</code></h4>
288<ul>
289<li>Default: "1.0.0"</li>
290<li>Type: SemVer string</li>
291</ul>
292<p>The value that <code>npm init</code> should use by default for the package version
293number, if not already set in package.json.</p>
294<h4 id="yes"><code>yes</code></h4>
295<ul>
296<li>Default: null</li>
297<li>Type: null or Boolean</li>
298</ul>
299<p>Automatically answer "yes" to any prompts that npm might print on the
300command line.</p>
301<h4 id="force"><code>force</code></h4>
302<ul>
303<li>Default: false</li>
304<li>Type: Boolean</li>
305</ul>
306<p>Removes various protections against unfortunate side effects, common
307mistakes, unnecessary performance degradation, and malicious input.</p>
308<ul>
309<li>Allow clobbering non-npm files in global installs.</li>
310<li>Allow the <code>npm version</code> command to work on an unclean git repository.</li>
311<li>Allow deleting the cache folder with <code>npm cache clean</code>.</li>
312<li>Allow installing packages that have an <code>engines</code> declaration requiring a
313different version of npm.</li>
314<li>Allow installing packages that have an <code>engines</code> declaration requiring a
315different version of <code>node</code>, even if <code>--engine-strict</code> is enabled.</li>
316<li>Allow <code>npm audit fix</code> to install modules outside your stated dependency
317range (including SemVer-major changes).</li>
318<li>Allow unpublishing all versions of a published package.</li>
319<li>Allow conflicting peerDependencies to be installed in the root project.</li>
320<li>Implicitly set <code>--yes</code> during <code>npm init</code>.</li>
321<li>Allow clobbering existing values in <code>npm pkg</code></li>
322<li>Allow unpublishing of entire packages (not just a single version).</li>
323</ul>
324<p>If you don't have a clear idea of what you want to do, it is strongly
325recommended that you do not use this option!</p>
326<h4 id="scope"><code>scope</code></h4>
327<ul>
328<li>Default: the scope of the current project, if any, or ""</li>
329<li>Type: String</li>
330</ul>
331<p>Associate an operation with a scope for a scoped registry.</p>
332<p>Useful when logging in to or out of a private registry:</p>
333<pre><code># log in, linking the scope to the custom registry
334npm login --scope=@mycorp --registry=https://registry.mycorp.com
335
336# log out, removing the link and the auth token
337npm logout --scope=@mycorp
338</code></pre>
339<p>This will cause <code>@mycorp</code> to be mapped to the registry for future
340installation of packages specified according to the pattern
341<code>@mycorp/package</code>.</p>
342<p>This will also cause <code>npm init</code> to create a scoped package.</p>
343<pre><code># accept all defaults, and create a package named "@foo/whatever",
344# instead of just named "whatever"
345npm init --scope=@foo --yes
346</code></pre>
347<h4 id="workspace"><code>workspace</code></h4>
348<ul>
349<li>Default:</li>
350<li>Type: String (can be set multiple times)</li>
351</ul>
352<p>Enable running a command in the context of the configured workspaces of the
353current project while filtering by running only the workspaces defined by
354this configuration option.</p>
355<p>Valid values for the <code>workspace</code> config are either:</p>
356<ul>
357<li>Workspace names</li>
358<li>Path to a workspace directory</li>
359<li>Path to a parent workspace directory (will result in selecting all
360workspaces within that folder)</li>
361</ul>
362<p>When set for the <code>npm init</code> command, this may be set to the folder of a
363workspace which does not yet exist, to create the folder and set it up as a
364brand new workspace within the project.</p>
365<p>This value is not exported to the environment for child processes.</p>
366<h4 id="workspaces"><code>workspaces</code></h4>
367<ul>
368<li>Default: null</li>
369<li>Type: null or Boolean</li>
370</ul>
371<p>Set to true to run the command in the context of <strong>all</strong> configured
372workspaces.</p>
373<p>Explicitly setting this to false will cause commands like <code>install</code> to
374ignore workspaces altogether. When not set explicitly:</p>
375<ul>
376<li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
377will link workspaces into the <code>node_modules</code> folder. - Commands that do
378other things (test, exec, publish, etc.) will operate on the root project,
379<em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li>
380</ul>
381<p>This value is not exported to the environment for child processes.</p>
382<h4 id="workspaces-update"><code>workspaces-update</code></h4>
383<ul>
384<li>Default: true</li>
385<li>Type: Boolean</li>
386</ul>
387<p>If set to true, the npm cli will run an update after operations that may
388possibly change the workspaces installed to the <code>node_modules</code> folder.</p>
389<h4 id="include-workspace-root"><code>include-workspace-root</code></h4>
390<ul>
391<li>Default: false</li>
392<li>Type: Boolean</li>
393</ul>
394<p>Include the workspace root when workspaces are enabled for a command.</p>
395<p>When false, specifying individual workspaces via the <code>workspace</code> config, or
396all workspaces via the <code>workspaces</code> flag, will cause npm to operate only on
397the specified workspaces, and not on the root project.</p>
398<p>This value is not exported to the environment for child processes.</p>
399<h3 id="see-also">See Also</h3>
400<ul>
401<li><a href="../using-npm/package-spec.html">package spec</a></li>
402<li><a href="http://npm.im/init-package-json">init-package-json module</a></li>
403<li><a href="../configuring-npm/package-json.html">package.json</a></li>
404<li><a href="../commands/npm-version.html">npm version</a></li>
405<li><a href="../using-npm/scope.html">npm scope</a></li>
406<li><a href="../commands/npm-exec.html">npm exec</a></li>
407<li><a href="../using-npm/workspaces.html">npm workspaces</a></li>
408</ul></div>
409
410<footer id="edit">
411<a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-init.md">
412<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
413<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>
414</svg>
415Edit this page on GitHub
416</a>
417</footer>
418</section>
419
420
421
422</body></html>