developers.7 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. .TH "DEVELOPERS" "7" "September 2021" "" ""
  2. .SH "NAME"
  3. \fBdevelopers\fR \- Developer Guide
  4. .SS Description
  5. .P
  6. So, you've decided to use npm to develop (and maybe publish/deploy)
  7. your project\.
  8. .P
  9. Fantastic!
  10. .P
  11. There are a few things that you need to do above the simple steps
  12. that your users will do to install your program\.
  13. .SS About These Documents
  14. .P
  15. These are man pages\. If you install npm, you should be able to
  16. then do \fBman npm\-thing\fP to get the documentation on a particular
  17. topic, or \fBnpm help thing\fP to see the same information\.
  18. .SS What is a Package
  19. .P
  20. A package is:
  21. .RS 0
  22. .IP \(bu 2
  23. a) a folder containing a program described by a package\.json file
  24. .IP \(bu 2
  25. b) a gzipped tarball containing (a)
  26. .IP \(bu 2
  27. c) a url that resolves to (b)
  28. .IP \(bu 2
  29. d) a \fB<name>@<version>\fP that is published on the registry with (c)
  30. .IP \(bu 2
  31. e) a \fB<name>@<tag>\fP that points to (d)
  32. .IP \(bu 2
  33. f) a \fB<name>\fP that has a "latest" tag satisfying (e)
  34. .IP \(bu 2
  35. g) a \fBgit\fP url that, when cloned, results in (a)\.
  36. .RE
  37. .P
  38. Even if you never publish your package, you can still get a lot of
  39. benefits of using npm if you just want to write a node program (a), and
  40. perhaps if you also want to be able to easily install it elsewhere
  41. after packing it up into a tarball (b)\.
  42. .P
  43. Git urls can be of the form:
  44. .P
  45. .RS 2
  46. .nf
  47. git://github\.com/user/project\.git#commit\-ish
  48. git+ssh://user@hostname:project\.git#commit\-ish
  49. git+http://user@hostname/project/blah\.git#commit\-ish
  50. git+https://user@hostname/project/blah\.git#commit\-ish
  51. .fi
  52. .RE
  53. .P
  54. The \fBcommit\-ish\fP can be any tag, sha, or branch which can be supplied as
  55. an argument to \fBgit checkout\fP\|\. The default is whatever the repository uses
  56. as its default branch\.
  57. .SS The package\.json File
  58. .P
  59. You need to have a \fBpackage\.json\fP file in the root of your project to do
  60. much of anything with npm\. That is basically the whole interface\.
  61. .P
  62. See npm help \fBpackage\.json\fP for details about what
  63. goes in that file\. At the very least, you need:
  64. .RS 0
  65. .IP \(bu 2
  66. name: This should be a string that identifies your project\. Please do
  67. not use the name to specify that it runs on node, or is in JavaScript\.
  68. You can use the "engines" field to explicitly state the versions of node
  69. (or whatever else) that your program requires, and it's pretty well
  70. assumed that it's JavaScript\.
  71. It does not necessarily need to match your github repository name\.
  72. So, \fBnode\-foo\fP and \fBbar\-js\fP are bad names\. \fBfoo\fP or \fBbar\fP are better\.
  73. .IP \(bu 2
  74. version: A semver\-compatible version\.
  75. .IP \(bu 2
  76. engines: Specify the versions of node (or whatever else) that your
  77. program runs on\. The node API changes a lot, and there may be bugs or
  78. new functionality that you depend on\. Be explicit\.
  79. .IP \(bu 2
  80. author: Take some credit\.
  81. .IP \(bu 2
  82. scripts: If you have a special compilation or installation script, then
  83. you should put it in the \fBscripts\fP object\. You should definitely have at
  84. least a basic smoke\-test command as the "scripts\.test" field\. See
  85. npm help scripts\.
  86. .IP \(bu 2
  87. main: If you have a single module that serves as the entry point to your
  88. program (like what the "foo" package gives you at require("foo")), then
  89. you need to specify that in the "main" field\.
  90. .IP \(bu 2
  91. directories: This is an object mapping names to folders\. The best ones
  92. to include are "lib" and "doc", but if you use "man" to specify a folder
  93. full of man pages, they'll get installed just like these ones\.
  94. .RE
  95. .P
  96. You can use \fBnpm init\fP in the root of your package in order to get you
  97. started with a pretty basic package\.json file\. See npm help \fBnpm
  98. init\fP for more info\.
  99. .SS Keeping files \fIout\fR of your Package
  100. .P
  101. Use a \fB\|\.npmignore\fP file to keep stuff out of your package\. If there's no
  102. \fB\|\.npmignore\fP file, but there \fIis\fR a \fB\|\.gitignore\fP file, then npm will ignore
  103. the stuff matched by the \fB\|\.gitignore\fP file\. If you \fIwant\fR to include
  104. something that is excluded by your \fB\|\.gitignore\fP file, you can create an
  105. empty \fB\|\.npmignore\fP file to override it\. Like \fBgit\fP, \fBnpm\fP looks for
  106. \fB\|\.npmignore\fP and \fB\|\.gitignore\fP files in all subdirectories of your package,
  107. not only the root directory\.
  108. .P
  109. \fB\|\.npmignore\fP files follow the same pattern
  110. rules \fIhttps://git\-scm\.com/book/en/v2/Git\-Basics\-Recording\-Changes\-to\-the\-Repository#_ignoring\fR
  111. as \fB\|\.gitignore\fP files:
  112. .RS 0
  113. .IP \(bu 2
  114. Blank lines or lines starting with \fB#\fP are ignored\.
  115. .IP \(bu 2
  116. Standard glob patterns work\.
  117. .IP \(bu 2
  118. You can end patterns with a forward slash \fB/\fP to specify a directory\.
  119. .IP \(bu 2
  120. You can negate a pattern by starting it with an exclamation point \fB!\fP\|\.
  121. .RE
  122. .P
  123. By default, the following paths and files are ignored, so there's no
  124. need to add them to \fB\|\.npmignore\fP explicitly:
  125. .RS 0
  126. .IP \(bu 2
  127. \fB\|\.*\.swp\fP
  128. .IP \(bu 2
  129. \fB\|\._*\fP
  130. .IP \(bu 2
  131. \fB\|\.DS_Store\fP
  132. .IP \(bu 2
  133. \fB\|\.git\fP
  134. .IP \(bu 2
  135. \fB\|\.hg\fP
  136. .IP \(bu 2
  137. \fB\|\.npmrc\fP
  138. .IP \(bu 2
  139. \fB\|\.lock\-wscript\fP
  140. .IP \(bu 2
  141. \fB\|\.svn\fP
  142. .IP \(bu 2
  143. \fB\|\.wafpickle\-*\fP
  144. .IP \(bu 2
  145. \fBconfig\.gypi\fP
  146. .IP \(bu 2
  147. \fBCVS\fP
  148. .IP \(bu 2
  149. \fBnpm\-debug\.log\fP
  150. .RE
  151. .P
  152. Additionally, everything in \fBnode_modules\fP is ignored, except for
  153. bundled dependencies\. npm automatically handles this for you, so don't
  154. bother adding \fBnode_modules\fP to \fB\|\.npmignore\fP\|\.
  155. .P
  156. The following paths and files are never ignored, so adding them to
  157. \fB\|\.npmignore\fP is pointless:
  158. .RS 0
  159. .IP \(bu 2
  160. \fBpackage\.json\fP
  161. .IP \(bu 2
  162. \fBREADME\fP (and its variants)
  163. .IP \(bu 2
  164. \fBCHANGELOG\fP (and its variants)
  165. .IP \(bu 2
  166. \fBLICENSE\fP / \fBLICENCE\fP
  167. .RE
  168. .P
  169. If, given the structure of your project, you find \fB\|\.npmignore\fP to be a
  170. maintenance headache, you might instead try populating the \fBfiles\fP
  171. property of \fBpackage\.json\fP, which is an array of file or directory names
  172. that should be included in your package\. Sometimes manually picking
  173. which items to allow is easier to manage than building a block list\.
  174. .SS Testing whether your \fB\|\.npmignore\fP or \fBfiles\fP config works
  175. .P
  176. If you want to double check that your package will include only the files
  177. you intend it to when published, you can run the \fBnpm pack\fP command locally
  178. which will generate a tarball in the working directory, the same way it
  179. does for publishing\.
  180. .SS Link Packages
  181. .P
  182. \fBnpm link\fP is designed to install a development package and see the
  183. changes in real time without having to keep re\-installing it\. (You do
  184. need to either re\-link or \fBnpm rebuild \-g\fP to update compiled packages,
  185. of course\.)
  186. .P
  187. More info at npm help \fBlink\fP\|\.
  188. .SS Before Publishing: Make Sure Your Package Installs and Works
  189. .P
  190. \fBThis is important\.\fR
  191. .P
  192. If you can not install it locally, you'll have
  193. problems trying to publish it\. Or, worse yet, you'll be able to
  194. publish it, but you'll be publishing a broken or pointless package\.
  195. So don't do that\.
  196. .P
  197. In the root of your package, do this:
  198. .P
  199. .RS 2
  200. .nf
  201. npm install \. \-g
  202. .fi
  203. .RE
  204. .P
  205. That'll show you that it's working\. If you'd rather just create a symlink
  206. package that points to your working directory, then do this:
  207. .P
  208. .RS 2
  209. .nf
  210. npm link
  211. .fi
  212. .RE
  213. .P
  214. Use \fBnpm ls \-g\fP to see if it's there\.
  215. .P
  216. To test a local install, go into some other folder, and then do:
  217. .P
  218. .RS 2
  219. .nf
  220. cd \.\./some\-other\-folder
  221. npm install \.\./my\-package
  222. .fi
  223. .RE
  224. .P
  225. to install it locally into the node_modules folder in that other place\.
  226. .P
  227. Then go into the node\-repl, and try using require("my\-thing") to
  228. bring in your module's main module\.
  229. .SS Create a User Account
  230. .P
  231. Create a user with the adduser command\. It works like this:
  232. .P
  233. .RS 2
  234. .nf
  235. npm adduser
  236. .fi
  237. .RE
  238. .P
  239. and then follow the prompts\.
  240. .P
  241. This is documented better in npm help adduser\.
  242. .SS Publish your Package
  243. .P
  244. This part's easy\. In the root of your folder, do this:
  245. .P
  246. .RS 2
  247. .nf
  248. npm publish
  249. .fi
  250. .RE
  251. .P
  252. You can give publish a url to a tarball, or a filename of a tarball,
  253. or a path to a folder\.
  254. .P
  255. Note that pretty much \fBeverything in that folder will be exposed\fR
  256. by default\. So, if you have secret stuff in there, use a
  257. \fB\|\.npmignore\fP file to list out the globs to ignore, or publish
  258. from a fresh checkout\.
  259. .SS Brag about it
  260. .P
  261. Send emails, write blogs, blab in IRC\.
  262. .P
  263. Tell the world how easy it is to install your program!
  264. .SS See also
  265. .RS 0
  266. .IP \(bu 2
  267. npm help npm
  268. .IP \(bu 2
  269. npm help init
  270. .IP \(bu 2
  271. npm help package\.json
  272. .IP \(bu 2
  273. npm help scripts
  274. .IP \(bu 2
  275. npm help publish
  276. .IP \(bu 2
  277. npm help adduser
  278. .IP \(bu 2
  279. npm help registry
  280. .RE