developers.html 14 KB

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