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