developers.7 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. .TH "DEVELOPERS" "7" "August 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 \fBmaster\fP\|\.
  56. .SS The package\.json File
  57. .P
  58. You need to have a \fBpackage\.json\fP file in the root of your project to do
  59. much of anything with npm\. That is basically the whole interface\.
  60. .P
  61. See npm help \fBpackage\.json\fP for details about what goes in that file\. At the very
  62. least, you need:
  63. .RS 0
  64. .IP \(bu 2
  65. name:
  66. This should be a string that identifies your project\. Please do not
  67. 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
  69. node (or whatever else) that your program requires, and it's pretty
  70. well 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:
  75. A semver\-compatible version\.
  76. .IP \(bu 2
  77. engines:
  78. Specify the versions of node (or whatever else) that your program
  79. runs on\. The node API changes a lot, and there may be bugs or new
  80. functionality that you depend on\. Be explicit\.
  81. .IP \(bu 2
  82. author:
  83. Take some credit\.
  84. .IP \(bu 2
  85. scripts:
  86. If you have a special compilation or installation script, then you
  87. should put it in the \fBscripts\fP object\. You should definitely have at
  88. least a basic smoke\-test command as the "scripts\.test" field\.
  89. See npm help scripts\.
  90. .IP \(bu 2
  91. main:
  92. If you have a single module that serves as the entry point to your
  93. program (like what the "foo" package gives you at require("foo")),
  94. then you need to specify that in the "main" field\.
  95. .IP \(bu 2
  96. directories:
  97. This is an object mapping names to folders\. The best ones to include are
  98. "lib" and "doc", but if you use "man" to specify a folder full of man pages,
  99. they'll get installed just like these ones\.
  100. .RE
  101. .P
  102. You can use \fBnpm init\fP in the root of your package in order to get you
  103. started with a pretty basic package\.json file\. See npm help \fBinit\fP for
  104. more info\.
  105. .SS Keeping files \fIout\fR of your package
  106. .P
  107. Use a \fB\|\.npmignore\fP file to keep stuff out of your package\. If there's
  108. no \fB\|\.npmignore\fP file, but there \fIis\fR a \fB\|\.gitignore\fP file, then npm will
  109. ignore the stuff matched by the \fB\|\.gitignore\fP file\. If you \fIwant\fR to
  110. include something that is excluded by your \fB\|\.gitignore\fP file, you can
  111. create an empty \fB\|\.npmignore\fP file to override it\. Like \fBgit\fP, \fBnpm\fP looks
  112. for \fB\|\.npmignore\fP and \fB\|\.gitignore\fP files in all subdirectories of your
  113. package, not only the root directory\.
  114. .P
  115. \fB\|\.npmignore\fP files follow the same pattern rules \fIhttps://git\-scm\.com/book/en/v2/Git\-Basics\-Recording\-Changes\-to\-the\-Repository#Ignoring\-Files\fR
  116. as \fB\|\.gitignore\fP files:
  117. .RS 0
  118. .IP \(bu 2
  119. Blank lines or lines starting with \fB#\fP are ignored\.
  120. .IP \(bu 2
  121. Standard glob patterns work\.
  122. .IP \(bu 2
  123. You can end patterns with a forward slash \fB/\fP to specify a directory\.
  124. .IP \(bu 2
  125. You can negate a pattern by starting it with an exclamation point \fB!\fP\|\.
  126. .RE
  127. .P
  128. By default, the following paths and files are ignored, so there's no
  129. need to add them to \fB\|\.npmignore\fP explicitly:
  130. .RS 0
  131. .IP \(bu 2
  132. \fB\|\.*\.swp\fP
  133. .IP \(bu 2
  134. \fB\|\._*\fP
  135. .IP \(bu 2
  136. \fB\|\.DS_Store\fP
  137. .IP \(bu 2
  138. \fB\|\.git\fP
  139. .IP \(bu 2
  140. \fB\|\.hg\fP
  141. .IP \(bu 2
  142. \fB\|\.npmrc\fP
  143. .IP \(bu 2
  144. \fB\|\.lock\-wscript\fP
  145. .IP \(bu 2
  146. \fB\|\.svn\fP
  147. .IP \(bu 2
  148. \fB\|\.wafpickle\-*\fP
  149. .IP \(bu 2
  150. \fBconfig\.gypi\fP
  151. .IP \(bu 2
  152. \fBCVS\fP
  153. .IP \(bu 2
  154. \fBnpm\-debug\.log\fP
  155. .RE
  156. .P
  157. Additionally, everything in \fBnode_modules\fP is ignored, except for
  158. bundled dependencies\. npm automatically handles this for you, so don't
  159. bother adding \fBnode_modules\fP to \fB\|\.npmignore\fP\|\.
  160. .P
  161. The following paths and files are never ignored, so adding them to
  162. \fB\|\.npmignore\fP is pointless:
  163. .RS 0
  164. .IP \(bu 2
  165. \fBpackage\.json\fP
  166. .IP \(bu 2
  167. \fBREADME\fP (and its variants)
  168. .IP \(bu 2
  169. \fBCHANGELOG\fP (and its variants)
  170. .IP \(bu 2
  171. \fBLICENSE\fP / \fBLICENCE\fP
  172. .RE
  173. .P
  174. If, given the structure of your project, you find \fB\|\.npmignore\fP to be a
  175. maintenance headache, you might instead try populating the \fBfiles\fP
  176. property of \fBpackage\.json\fP, which is an array of file or directory names
  177. that should be included in your package\. Sometimes a whitelist is easier
  178. to manage than a blacklist\.
  179. .SS Testing whether your \fB\|\.npmignore\fP or \fBfiles\fP config works
  180. .P
  181. If you want to double check that your package will include only the files
  182. you intend it to when published, you can run the \fBnpm pack\fP command locally
  183. which will generate a tarball in the working directory, the same way it
  184. does for publishing\.
  185. .SS Link Packages
  186. .P
  187. \fBnpm link\fP is designed to install a development package and see the
  188. changes in real time without having to keep re\-installing it\. (You do
  189. need to either re\-link or \fBnpm rebuild \-g\fP to update compiled packages,
  190. of course\.)
  191. .P
  192. More info at npm help \fBlink\fP\|\.
  193. .SS Before Publishing: Make Sure Your Package Installs and Works
  194. .P
  195. \fBThis is important\.\fR
  196. .P
  197. If you can not install it locally, you'll have
  198. problems trying to publish it\. Or, worse yet, you'll be able to
  199. publish it, but you'll be publishing a broken or pointless package\.
  200. So don't do that\.
  201. .P
  202. In the root of your package, do this:
  203. .P
  204. .RS 2
  205. .nf
  206. npm install \. \-g
  207. .fi
  208. .RE
  209. .P
  210. That'll show you that it's working\. If you'd rather just create a symlink
  211. package that points to your working directory, then do this:
  212. .P
  213. .RS 2
  214. .nf
  215. npm link
  216. .fi
  217. .RE
  218. .P
  219. Use \fBnpm ls \-g\fP to see if it's there\.
  220. .P
  221. To test a local install, go into some other folder, and then do:
  222. .P
  223. .RS 2
  224. .nf
  225. cd \.\./some\-other\-folder
  226. npm install \.\./my\-package
  227. .fi
  228. .RE
  229. .P
  230. to install it locally into the node_modules folder in that other place\.
  231. .P
  232. Then go into the node\-repl, and try using require("my\-thing") to
  233. bring in your module's main module\.
  234. .SS Create a User Account
  235. .P
  236. Create a user with the adduser command\. It works like this:
  237. .P
  238. .RS 2
  239. .nf
  240. npm adduser
  241. .fi
  242. .RE
  243. .P
  244. and then follow the prompts\.
  245. .P
  246. This is documented better in npm help adduser\.
  247. .SS Publish your package
  248. .P
  249. This part's easy\. In the root of your folder, do this:
  250. .P
  251. .RS 2
  252. .nf
  253. npm publish
  254. .fi
  255. .RE
  256. .P
  257. You can give publish a url to a tarball, or a filename of a tarball,
  258. or a path to a folder\.
  259. .P
  260. Note that pretty much \fBeverything in that folder will be exposed\fR
  261. by default\. So, if you have secret stuff in there, use a
  262. \fB\|\.npmignore\fP file to list out the globs to ignore, or publish
  263. from a fresh checkout\.
  264. .SS Brag about it
  265. .P
  266. Send emails, write blogs, blab in IRC\.
  267. .P
  268. Tell the world how easy it is to install your program!
  269. .SS See also
  270. .RS 0
  271. .IP \(bu 2
  272. npm help npm
  273. .IP \(bu 2
  274. npm help init
  275. .IP \(bu 2
  276. npm help package\.json
  277. .IP \(bu 2
  278. npm help scripts
  279. .IP \(bu 2
  280. npm help publish
  281. .IP \(bu 2
  282. npm help adduser
  283. .IP \(bu 2
  284. npm help registry
  285. .RE