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