• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html><html><head>
2<meta charset="utf-8">
3<title>developers</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="developers">developers</h1>
140<span class="description">Developer Guide</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="#description">Description</a></li><li><a href="#about-these-documents">About These Documents</a></li><li><a href="#what-is-a-package">What is a Package</a></li><li><a href="#the-packagejson-file">The package.json File</a></li><li><a href="#keeping-files-out-of-your-package">Keeping files <em>out</em> of your Package</a></li><ul><li><a href="#testing-whether-your-npmignore-or-files-config-works">Testing whether your <code>.npmignore</code> or <code>files</code> config works</a></li></ul><li><a href="#link-packages">Link Packages</a></li><li><a href="#before-publishing-make-sure-your-package-installs-and-works">Before Publishing: Make Sure Your Package Installs and Works</a></li><li><a href="#create-a-user-account">Create a User Account</a></li><li><a href="#publish-your-package">Publish your Package</a></li><li><a href="#brag-about-it">Brag about it</a></li><li><a href="#see-also">See also</a></li></ul></div>
146</section>
147
148<div id="_content"><h3 id="description">Description</h3>
149<p>So, you've decided to use npm to develop (and maybe publish/deploy)
150your project.</p>
151<p>Fantastic!</p>
152<p>There are a few things that you need to do above the simple steps
153that your users will do to install your program.</p>
154<h3 id="about-these-documents">About These Documents</h3>
155<p>These are man pages.  If you install npm, you should be able to
156then do <code>man npm-thing</code> to get the documentation on a particular
157topic, or <code>npm help thing</code> to see the same information.</p>
158<h3 id="what-is-a-package">What is a Package</h3>
159<p>A package is:</p>
160<ul>
161<li>a) a folder containing a program described by a package.json file</li>
162<li>b) a gzipped tarball containing (a)</li>
163<li>c) a url that resolves to (b)</li>
164<li>d) a <code>&lt;name&gt;@&lt;version&gt;</code> that is published on the registry with (c)</li>
165<li>e) a <code>&lt;name&gt;@&lt;tag&gt;</code> that points to (d)</li>
166<li>f) a <code>&lt;name&gt;</code> that has a "latest" tag satisfying (e)</li>
167<li>g) a <code>git</code> url that, when cloned, results in (a).</li>
168</ul>
169<p>Even if you never publish your package, you can still get a lot of
170benefits of using npm if you just want to write a node program (a), and
171perhaps if you also want to be able to easily install it elsewhere
172after packing it up into a tarball (b).</p>
173<p>Git urls can be of the form:</p>
174<pre><code class="language-bash">git://github.com/user/project.git#commit-ish
175git+ssh://user@hostname:project.git#commit-ish
176git+http://user@hostname/project/blah.git#commit-ish
177git+https://user@hostname/project/blah.git#commit-ish
178</code></pre>
179<p>The <code>commit-ish</code> can be any tag, sha, or branch which can be supplied as
180an argument to <code>git checkout</code>.  The default is whatever the repository uses
181as its default branch.</p>
182<h3 id="the-packagejson-file">The package.json File</h3>
183<p>You need to have a <code>package.json</code> file in the root of your project to do
184much of anything with npm.  That is basically the whole interface.</p>
185<p>See <a href="../configuring-npm/package-json.html"><code>package.json</code></a> for details about what
186goes in that file.  At the very least, you need:</p>
187<ul>
188<li>
189<p>name: This should be a string that identifies your project.  Please do
190not use the name to specify that it runs on node, or is in JavaScript.
191You can use the "engines" field to explicitly state the versions of node
192(or whatever else) that your program requires, and it's pretty well
193assumed that it's JavaScript.</p>
194<p>It does not necessarily need to match your github repository name.</p>
195<p>So, <code>node-foo</code> and <code>bar-js</code> are bad names.  <code>foo</code> or <code>bar</code> are better.</p>
196</li>
197<li>
198<p>version: A semver-compatible version.</p>
199</li>
200<li>
201<p>engines: Specify the versions of node (or whatever else) that your
202program runs on.  The node API changes a lot, and there may be bugs or
203new functionality that you depend on.  Be explicit.</p>
204</li>
205<li>
206<p>author: Take some credit.</p>
207</li>
208<li>
209<p>scripts: If you have a special compilation or installation script, then
210you should put it in the <code>scripts</code> object.  You should definitely have at
211least a basic smoke-test command as the "scripts.test" field.  See
212<a href="../using-npm/scripts.html">scripts</a>.</p>
213</li>
214<li>
215<p>main: If you have a single module that serves as the entry point to your
216program (like what the "foo" package gives you at require("foo")), then
217you need to specify that in the "main" field.</p>
218</li>
219<li>
220<p>directories: This is an object mapping names to folders.  The best ones
221to include are "lib" and "doc", but if you use "man" to specify a folder
222full of man pages, they'll get installed just like these ones.</p>
223</li>
224</ul>
225<p>You can use <code>npm init</code> in the root of your package in order to get you
226started with a pretty basic package.json file.  See <a href="../commands/npm-init.html"><code>npm init</code></a> for more info.</p>
227<h3 id="keeping-files-out-of-your-package">Keeping files <em>out</em> of your Package</h3>
228<p>Use a <code>.npmignore</code> file to keep stuff out of your package.  If there's no
229<code>.npmignore</code> file, but there <em>is</em> a <code>.gitignore</code> file, then npm will ignore
230the stuff matched by the <code>.gitignore</code> file.  If you <em>want</em> to include
231something that is excluded by your <code>.gitignore</code> file, you can create an
232empty <code>.npmignore</code> file to override it. Like <code>git</code>, <code>npm</code> looks for
233<code>.npmignore</code> and <code>.gitignore</code> files in all subdirectories of your package,
234not only the root directory.</p>
235<p><code>.npmignore</code> files follow the <a href="https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#_ignoring">same pattern
236rules</a>
237as <code>.gitignore</code> files:</p>
238<ul>
239<li>Blank lines or lines starting with <code>#</code> are ignored.</li>
240<li>Standard glob patterns work.</li>
241<li>You can end patterns with a forward slash <code>/</code> to specify a directory.</li>
242<li>You can negate a pattern by starting it with an exclamation point <code>!</code>.</li>
243</ul>
244<p>By default, the following paths and files are ignored, so there's no
245need to add them to <code>.npmignore</code> explicitly:</p>
246<ul>
247<li><code>.*.swp</code></li>
248<li><code>._*</code></li>
249<li><code>.DS_Store</code></li>
250<li><code>.git</code></li>
251<li><code>.gitignore</code></li>
252<li><code>.hg</code></li>
253<li><code>.npmignore</code></li>
254<li><code>.npmrc</code></li>
255<li><code>.lock-wscript</code></li>
256<li><code>.svn</code></li>
257<li><code>.wafpickle-*</code></li>
258<li><code>config.gypi</code></li>
259<li><code>CVS</code></li>
260<li><code>npm-debug.log</code></li>
261</ul>
262<p>Additionally, everything in <code>node_modules</code> is ignored, except for
263bundled dependencies. npm automatically handles this for you, so don't
264bother adding <code>node_modules</code> to <code>.npmignore</code>.</p>
265<p>The following paths and files are never ignored, so adding them to
266<code>.npmignore</code> is pointless:</p>
267<ul>
268<li><code>package.json</code></li>
269<li><code>README</code> (and its variants)</li>
270<li><code>CHANGELOG</code> (and its variants)</li>
271<li><code>LICENSE</code> / <code>LICENCE</code></li>
272</ul>
273<p>If, given the structure of your project, you find <code>.npmignore</code> to be a
274maintenance headache, you might instead try populating the <code>files</code>
275property of <code>package.json</code>, which is an array of file or directory names
276that should be included in your package. Sometimes manually picking
277which items to allow is easier to manage than building a block list.</p>
278<h4 id="testing-whether-your-npmignore-or-files-config-works">Testing whether your <code>.npmignore</code> or <code>files</code> config works</h4>
279<p>If you want to double check that your package will include only the files
280you intend it to when published, you can run the <code>npm pack</code> command locally
281which will generate a tarball in the working directory, the same way it
282does for publishing.</p>
283<h3 id="link-packages">Link Packages</h3>
284<p><code>npm link</code> is designed to install a development package and see the
285changes in real time without having to keep re-installing it.  (You do
286need to either re-link or <code>npm rebuild -g</code> to update compiled packages,
287of course.)</p>
288<p>More info at <a href="../commands/npm-link.html"><code>npm link</code></a>.</p>
289<h3 id="before-publishing-make-sure-your-package-installs-and-works">Before Publishing: Make Sure Your Package Installs and Works</h3>
290<p><strong>This is important.</strong></p>
291<p>If you can not install it locally, you'll have
292problems trying to publish it.  Or, worse yet, you'll be able to
293publish it, but you'll be publishing a broken or pointless package.
294So don't do that.</p>
295<p>In the root of your package, do this:</p>
296<pre><code class="language-bash">npm install . -g
297</code></pre>
298<p>That'll show you that it's working.  If you'd rather just create a symlink
299package that points to your working directory, then do this:</p>
300<pre><code class="language-bash">npm link
301</code></pre>
302<p>Use <code>npm ls -g</code> to see if it's there.</p>
303<p>To test a local install, go into some other folder, and then do:</p>
304<pre><code class="language-bash">cd ../some-other-folder
305npm install ../my-package
306</code></pre>
307<p>to install it locally into the node_modules folder in that other place.</p>
308<p>Then go into the node-repl, and try using require("my-thing") to
309bring in your module's main module.</p>
310<h3 id="create-a-user-account">Create a User Account</h3>
311<p>Create a user with the adduser command.  It works like this:</p>
312<pre><code class="language-bash">npm adduser
313</code></pre>
314<p>and then follow the prompts.</p>
315<p>This is documented better in <a href="../commands/npm-adduser.html">npm adduser</a>.</p>
316<h3 id="publish-your-package">Publish your Package</h3>
317<p>This part's easy.  In the root of your folder, do this:</p>
318<pre><code class="language-bash">npm publish
319</code></pre>
320<p>You can give publish a url to a tarball, or a filename of a tarball,
321or a path to a folder.</p>
322<p>Note that pretty much <strong>everything in that folder will be exposed</strong>
323by default.  So, if you have secret stuff in there, use a
324<code>.npmignore</code> file to list out the globs to ignore, or publish
325from a fresh checkout.</p>
326<h3 id="brag-about-it">Brag about it</h3>
327<p>Send emails, write blogs, blab in IRC.</p>
328<p>Tell the world how easy it is to install your program!</p>
329<h3 id="see-also">See also</h3>
330<ul>
331<li><a href="../commands/npm.html">npm</a></li>
332<li><a href="../commands/npm-init.html">npm init</a></li>
333<li><a href="../configuring-npm/package-json.html">package.json</a></li>
334<li><a href="../using-npm/scripts.html">npm scripts</a></li>
335<li><a href="../commands/npm-publish.html">npm publish</a></li>
336<li><a href="../commands/npm-adduser.html">npm adduser</a></li>
337<li><a href="../using-npm/registry.html">npm registry</a></li>
338</ul></div>
339
340<footer id="edit">
341<a href="https://github.com/npm/cli/edit/latest/docs/content/using-npm/developers.md">
342<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
343<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>
344</svg>
345Edit this page on GitHub
346</a>
347</footer>
348</section>
349
350
351
352</body></html>