1 npm-developers(7) -- Developer Guide
2 ====================================
6 So, you've decided to use npm to develop (and maybe publish/deploy)
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.
14 ## About These Documents
16 These are man pages. If you install npm, you should be able to
17 then do `man npm-thing` to get the documentation on a particular
18 topic, or `npm help thing` to see the same information.
20 ## What is a `package`
24 * a) a folder containing a program described by a package.json file
25 * b) a gzipped tarball containing (a)
26 * c) a url that resolves to (b)
27 * d) a `<name>@<version>` that is published on the registry with (c)
28 * e) a `<name>@<tag>` that points to (d)
29 * f) a `<name>` that has a "latest" tag satisfying (e)
30 * g) a `git` url that, when cloned, results in (a).
32 Even if you never publish your package, you can still get a lot of
33 benefits of using npm if you just want to write a node program (a), and
34 perhaps if you also want to be able to easily install it elsewhere
35 after packing it up into a tarball (b).
37 Git urls can be of the form:
39 git://github.com/user/project.git#commit-ish
40 git+ssh://user@hostname:project.git#commit-ish
41 git+http://user@hostname/project/blah.git#commit-ish
42 git+https://user@hostname/project/blah.git#commit-ish
44 The `commit-ish` can be any tag, sha, or branch which can be supplied as
45 an argument to `git checkout`. The default is `master`.
47 ## The package.json File
49 You need to have a `package.json` file in the root of your project to do
50 much of anything with npm. That is basically the whole interface.
52 See `package.json(5)` for details about what goes in that file. At the very
56 This should be a string that identifies your project. Please do not
57 use the name to specify that it runs on node, or is in JavaScript.
58 You can use the "engines" field to explicitly state the versions of
59 node (or whatever else) that your program requires, and it's pretty
60 well assumed that it's javascript.
62 It does not necessarily need to match your github repository name.
64 So, `node-foo` and `bar-js` are bad names. `foo` or `bar` are better.
67 A semver-compatible version.
70 Specify the versions of node (or whatever else) that your program
71 runs on. The node API changes a lot, and there may be bugs or new
72 functionality that you depend on. Be explicit.
78 If you have a special compilation or installation script, then you
79 should put it in the `scripts` object. You should definitely have at
80 least a basic smoke-test command as the "scripts.test" field.
84 If you have a single module that serves as the entry point to your
85 program (like what the "foo" package gives you at require("foo")),
86 then you need to specify that in the "main" field.
89 This is an object mapping names to folders. The best ones to include are
90 "lib" and "doc", but if you use "man" to specify a folder full of man pages,
91 they'll get installed just like these ones.
93 You can use `npm init` in the root of your package in order to get you
94 started with a pretty basic package.json file. See `npm-init(1)` for
97 ## Keeping files *out* of your package
99 Use a `.npmignore` file to keep stuff out of your package. If there's
100 no `.npmignore` file, but there *is* a `.gitignore` file, then npm will
101 ignore the stuff matched by the `.gitignore` file. If you *want* to
102 include something that is excluded by your `.gitignore` file, you can
103 create an empty `.npmignore` file to override it. Like `git`, `npm` looks
104 for `.npmignore` and `.gitignore` files in all subdirectories of your
105 package, not only the root directory.
107 `.npmignore` files follow the [same pattern rules](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#Ignoring-Files)
108 as `.gitignore` files:
110 * Blank lines or lines starting with `#` are ignored.
111 * Standard glob patterns work.
112 * You can end patterns with a forward slash `/` to specify a directory.
113 * You can negate a pattern by starting it with an exclamation point `!`.
115 By default, the following paths and files are ignored, so there's no
116 need to add them to `.npmignore` explicitly:
131 Additionally, everything in `node_modules` is ignored, except for
132 bundled dependencies. npm automatically handles this for you, so don't
133 bother adding `node_modules` to `.npmignore`.
135 The following paths and files are never ignored, so adding them to
136 `.npmignore` is pointless:
139 * `README` (and its variants)
140 * `CHANGELOG` (and its variants)
141 * `LICENSE` / `LICENCE`
145 `npm link` is designed to install a development package and see the
146 changes in real time without having to keep re-installing it. (You do
147 need to either re-link or `npm rebuild -g` to update compiled packages,
150 More info at `npm-link(1)`.
152 ## Before Publishing: Make Sure Your Package Installs and Works
154 **This is important.**
156 If you can not install it locally, you'll have
157 problems trying to publish it. Or, worse yet, you'll be able to
158 publish it, but you'll be publishing a broken or pointless package.
161 In the root of your package, do this:
165 That'll show you that it's working. If you'd rather just create a symlink
166 package that points to your working directory, then do this:
170 Use `npm ls -g` to see if it's there.
172 To test a local install, go into some other folder, and then do:
174 cd ../some-other-folder
175 npm install ../my-package
177 to install it locally into the node_modules folder in that other place.
179 Then go into the node-repl, and try using require("my-thing") to
180 bring in your module's main module.
182 ## Create a User Account
184 Create a user with the adduser command. It works like this:
188 and then follow the prompts.
190 This is documented better in npm-adduser(1).
192 ## Publish your package
194 This part's easy. In the root of your folder, do this:
198 You can give publish a url to a tarball, or a filename of a tarball,
199 or a path to a folder.
201 Note that pretty much **everything in that folder will be exposed**
202 by default. So, if you have secret stuff in there, use a
203 `.npmignore` file to list out the globs to ignore, or publish
204 from a fresh checkout.
208 Send emails, write blogs, blab in IRC.
210 Tell the world how easy it is to install your program!