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