• 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="#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="yes"><code>yes</code></h4>
260<ul>
261<li>Default: null</li>
262<li>Type: null or Boolean</li>
263</ul>
264<p>Automatically answer "yes" to any prompts that npm might print on the
265command line.</p>
266<h4 id="force"><code>force</code></h4>
267<ul>
268<li>Default: false</li>
269<li>Type: Boolean</li>
270</ul>
271<p>Removes various protections against unfortunate side effects, common
272mistakes, unnecessary performance degradation, and malicious input.</p>
273<ul>
274<li>Allow clobbering non-npm files in global installs.</li>
275<li>Allow the <code>npm version</code> command to work on an unclean git repository.</li>
276<li>Allow deleting the cache folder with <code>npm cache clean</code>.</li>
277<li>Allow installing packages that have an <code>engines</code> declaration requiring a
278different version of npm.</li>
279<li>Allow installing packages that have an <code>engines</code> declaration requiring a
280different version of <code>node</code>, even if <code>--engine-strict</code> is enabled.</li>
281<li>Allow <code>npm audit fix</code> to install modules outside your stated dependency
282range (including SemVer-major changes).</li>
283<li>Allow unpublishing all versions of a published package.</li>
284<li>Allow conflicting peerDependencies to be installed in the root project.</li>
285<li>Implicitly set <code>--yes</code> during <code>npm init</code>.</li>
286<li>Allow clobbering existing values in <code>npm pkg</code></li>
287<li>Allow unpublishing of entire packages (not just a single version).</li>
288</ul>
289<p>If you don't have a clear idea of what you want to do, it is strongly
290recommended that you do not use this option!</p>
291<h4 id="scope"><code>scope</code></h4>
292<ul>
293<li>Default: the scope of the current project, if any, or ""</li>
294<li>Type: String</li>
295</ul>
296<p>Associate an operation with a scope for a scoped registry.</p>
297<p>Useful when logging in to or out of a private registry:</p>
298<pre><code># log in, linking the scope to the custom registry
299npm login --scope=@mycorp --registry=https://registry.mycorp.com
300
301# log out, removing the link and the auth token
302npm logout --scope=@mycorp
303</code></pre>
304<p>This will cause <code>@mycorp</code> to be mapped to the registry for future
305installation of packages specified according to the pattern
306<code>@mycorp/package</code>.</p>
307<p>This will also cause <code>npm init</code> to create a scoped package.</p>
308<pre><code># accept all defaults, and create a package named "@foo/whatever",
309# instead of just named "whatever"
310npm init --scope=@foo --yes
311</code></pre>
312<h4 id="workspace"><code>workspace</code></h4>
313<ul>
314<li>Default:</li>
315<li>Type: String (can be set multiple times)</li>
316</ul>
317<p>Enable running a command in the context of the configured workspaces of the
318current project while filtering by running only the workspaces defined by
319this configuration option.</p>
320<p>Valid values for the <code>workspace</code> config are either:</p>
321<ul>
322<li>Workspace names</li>
323<li>Path to a workspace directory</li>
324<li>Path to a parent workspace directory (will result in selecting all
325workspaces within that folder)</li>
326</ul>
327<p>When set for the <code>npm init</code> command, this may be set to the folder of a
328workspace which does not yet exist, to create the folder and set it up as a
329brand new workspace within the project.</p>
330<p>This value is not exported to the environment for child processes.</p>
331<h4 id="workspaces"><code>workspaces</code></h4>
332<ul>
333<li>Default: null</li>
334<li>Type: null or Boolean</li>
335</ul>
336<p>Set to true to run the command in the context of <strong>all</strong> configured
337workspaces.</p>
338<p>Explicitly setting this to false will cause commands like <code>install</code> to
339ignore workspaces altogether. When not set explicitly:</p>
340<ul>
341<li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
342will link workspaces into the <code>node_modules</code> folder. - Commands that do
343other things (test, exec, publish, etc.) will operate on the root project,
344<em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li>
345</ul>
346<p>This value is not exported to the environment for child processes.</p>
347<h4 id="workspaces-update"><code>workspaces-update</code></h4>
348<ul>
349<li>Default: true</li>
350<li>Type: Boolean</li>
351</ul>
352<p>If set to true, the npm cli will run an update after operations that may
353possibly change the workspaces installed to the <code>node_modules</code> folder.</p>
354<h4 id="include-workspace-root"><code>include-workspace-root</code></h4>
355<ul>
356<li>Default: false</li>
357<li>Type: Boolean</li>
358</ul>
359<p>Include the workspace root when workspaces are enabled for a command.</p>
360<p>When false, specifying individual workspaces via the <code>workspace</code> config, or
361all workspaces via the <code>workspaces</code> flag, will cause npm to operate only on
362the specified workspaces, and not on the root project.</p>
363<p>This value is not exported to the environment for child processes.</p>
364<h3 id="see-also">See Also</h3>
365<ul>
366<li><a href="../using-npm/package-spec.html">package spec</a></li>
367<li><a href="http://npm.im/init-package-json">init-package-json module</a></li>
368<li><a href="../configuring-npm/package-json.html">package.json</a></li>
369<li><a href="../commands/npm-version.html">npm version</a></li>
370<li><a href="../using-npm/scope.html">npm scope</a></li>
371<li><a href="../commands/npm-exec.html">npm exec</a></li>
372<li><a href="../using-npm/workspaces.html">npm workspaces</a></li>
373</ul></div>
374
375<footer id="edit">
376<a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-init.md">
377<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
378<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>
379</svg>
380Edit this page on GitHub
381</a>
382</footer>
383</section>
384
385
386
387</body></html>