11cb0ef41Sopenharmony_ci--- 21cb0ef41Sopenharmony_cititle: developers 31cb0ef41Sopenharmony_cisection: 7 41cb0ef41Sopenharmony_cidescription: Developer Guide 51cb0ef41Sopenharmony_ci--- 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci### Description 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciSo, you've decided to use npm to develop (and maybe publish/deploy) 101cb0ef41Sopenharmony_ciyour project. 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciFantastic! 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ciThere are a few things that you need to do above the simple steps 151cb0ef41Sopenharmony_cithat your users will do to install your program. 161cb0ef41Sopenharmony_ci 171cb0ef41Sopenharmony_ci### About These Documents 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciThese are man pages. If you install npm, you should be able to 201cb0ef41Sopenharmony_cithen do `man npm-thing` to get the documentation on a particular 211cb0ef41Sopenharmony_citopic, or `npm help thing` to see the same information. 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci### What is a Package 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ciA package is: 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci* a) a folder containing a program described by a package.json file 281cb0ef41Sopenharmony_ci* b) a gzipped tarball containing (a) 291cb0ef41Sopenharmony_ci* c) a url that resolves to (b) 301cb0ef41Sopenharmony_ci* d) a `<name>@<version>` that is published on the registry with (c) 311cb0ef41Sopenharmony_ci* e) a `<name>@<tag>` that points to (d) 321cb0ef41Sopenharmony_ci* f) a `<name>` that has a "latest" tag satisfying (e) 331cb0ef41Sopenharmony_ci* g) a `git` url that, when cloned, results in (a). 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ciEven if you never publish your package, you can still get a lot of 361cb0ef41Sopenharmony_cibenefits of using npm if you just want to write a node program (a), and 371cb0ef41Sopenharmony_ciperhaps if you also want to be able to easily install it elsewhere 381cb0ef41Sopenharmony_ciafter packing it up into a tarball (b). 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciGit urls can be of the form: 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci```bash 431cb0ef41Sopenharmony_cigit://github.com/user/project.git#commit-ish 441cb0ef41Sopenharmony_cigit+ssh://user@hostname:project.git#commit-ish 451cb0ef41Sopenharmony_cigit+http://user@hostname/project/blah.git#commit-ish 461cb0ef41Sopenharmony_cigit+https://user@hostname/project/blah.git#commit-ish 471cb0ef41Sopenharmony_ci``` 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ciThe `commit-ish` can be any tag, sha, or branch which can be supplied as 501cb0ef41Sopenharmony_cian argument to `git checkout`. The default is whatever the repository uses 511cb0ef41Sopenharmony_cias its default branch. 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci### The package.json File 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ciYou need to have a `package.json` file in the root of your project to do 561cb0ef41Sopenharmony_cimuch of anything with npm. That is basically the whole interface. 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ciSee [`package.json`](/configuring-npm/package-json) for details about what 591cb0ef41Sopenharmony_cigoes in that file. At the very least, you need: 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci* name: This should be a string that identifies your project. Please do 621cb0ef41Sopenharmony_ci not use the name to specify that it runs on node, or is in JavaScript. 631cb0ef41Sopenharmony_ci You can use the "engines" field to explicitly state the versions of node 641cb0ef41Sopenharmony_ci (or whatever else) that your program requires, and it's pretty well 651cb0ef41Sopenharmony_ci assumed that it's JavaScript. 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci It does not necessarily need to match your github repository name. 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci So, `node-foo` and `bar-js` are bad names. `foo` or `bar` are better. 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_ci* version: A semver-compatible version. 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci* engines: Specify the versions of node (or whatever else) that your 741cb0ef41Sopenharmony_ci program runs on. The node API changes a lot, and there may be bugs or 751cb0ef41Sopenharmony_ci new functionality that you depend on. Be explicit. 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci* author: Take some credit. 781cb0ef41Sopenharmony_ci 791cb0ef41Sopenharmony_ci* scripts: If you have a special compilation or installation script, then 801cb0ef41Sopenharmony_ci you should put it in the `scripts` object. You should definitely have at 811cb0ef41Sopenharmony_ci least a basic smoke-test command as the "scripts.test" field. See 821cb0ef41Sopenharmony_ci [scripts](/using-npm/scripts). 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci* main: If you have a single module that serves as the entry point to your 851cb0ef41Sopenharmony_ci program (like what the "foo" package gives you at require("foo")), then 861cb0ef41Sopenharmony_ci you need to specify that in the "main" field. 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci* directories: This is an object mapping names to folders. The best ones 891cb0ef41Sopenharmony_ci to include are "lib" and "doc", but if you use "man" to specify a folder 901cb0ef41Sopenharmony_ci full of man pages, they'll get installed just like these ones. 911cb0ef41Sopenharmony_ci 921cb0ef41Sopenharmony_ciYou can use `npm init` in the root of your package in order to get you 931cb0ef41Sopenharmony_cistarted with a pretty basic package.json file. See [`npm 941cb0ef41Sopenharmony_ciinit`](/commands/npm-init) for more info. 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci### Keeping files *out* of your Package 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ciUse a `.npmignore` file to keep stuff out of your package. If there's no 991cb0ef41Sopenharmony_ci`.npmignore` file, but there *is* a `.gitignore` file, then npm will ignore 1001cb0ef41Sopenharmony_cithe stuff matched by the `.gitignore` file. If you *want* to include 1011cb0ef41Sopenharmony_cisomething that is excluded by your `.gitignore` file, you can create an 1021cb0ef41Sopenharmony_ciempty `.npmignore` file to override it. Like `git`, `npm` looks for 1031cb0ef41Sopenharmony_ci`.npmignore` and `.gitignore` files in all subdirectories of your package, 1041cb0ef41Sopenharmony_cinot only the root directory. 1051cb0ef41Sopenharmony_ci 1061cb0ef41Sopenharmony_ci`.npmignore` files follow the [same pattern 1071cb0ef41Sopenharmony_cirules](https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository#_ignoring) 1081cb0ef41Sopenharmony_cias `.gitignore` files: 1091cb0ef41Sopenharmony_ci 1101cb0ef41Sopenharmony_ci* Blank lines or lines starting with `#` are ignored. 1111cb0ef41Sopenharmony_ci* Standard glob patterns work. 1121cb0ef41Sopenharmony_ci* You can end patterns with a forward slash `/` to specify a directory. 1131cb0ef41Sopenharmony_ci* You can negate a pattern by starting it with an exclamation point `!`. 1141cb0ef41Sopenharmony_ci 1151cb0ef41Sopenharmony_ciBy default, the following paths and files are ignored, so there's no 1161cb0ef41Sopenharmony_cineed to add them to `.npmignore` explicitly: 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci* `.*.swp` 1191cb0ef41Sopenharmony_ci* `._*` 1201cb0ef41Sopenharmony_ci* `.DS_Store` 1211cb0ef41Sopenharmony_ci* `.git` 1221cb0ef41Sopenharmony_ci* `.gitignore` 1231cb0ef41Sopenharmony_ci* `.hg` 1241cb0ef41Sopenharmony_ci* `.npmignore` 1251cb0ef41Sopenharmony_ci* `.npmrc` 1261cb0ef41Sopenharmony_ci* `.lock-wscript` 1271cb0ef41Sopenharmony_ci* `.svn` 1281cb0ef41Sopenharmony_ci* `.wafpickle-*` 1291cb0ef41Sopenharmony_ci* `config.gypi` 1301cb0ef41Sopenharmony_ci* `CVS` 1311cb0ef41Sopenharmony_ci* `npm-debug.log` 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ciAdditionally, everything in `node_modules` is ignored, except for 1341cb0ef41Sopenharmony_cibundled dependencies. npm automatically handles this for you, so don't 1351cb0ef41Sopenharmony_cibother adding `node_modules` to `.npmignore`. 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ciThe following paths and files are never ignored, so adding them to 1381cb0ef41Sopenharmony_ci`.npmignore` is pointless: 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci* `package.json` 1411cb0ef41Sopenharmony_ci* `README` (and its variants) 1421cb0ef41Sopenharmony_ci* `CHANGELOG` (and its variants) 1431cb0ef41Sopenharmony_ci* `LICENSE` / `LICENCE` 1441cb0ef41Sopenharmony_ci 1451cb0ef41Sopenharmony_ciIf, given the structure of your project, you find `.npmignore` to be a 1461cb0ef41Sopenharmony_cimaintenance headache, you might instead try populating the `files` 1471cb0ef41Sopenharmony_ciproperty of `package.json`, which is an array of file or directory names 1481cb0ef41Sopenharmony_cithat should be included in your package. Sometimes manually picking 1491cb0ef41Sopenharmony_ciwhich items to allow is easier to manage than building a block list. 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci#### Testing whether your `.npmignore` or `files` config works 1521cb0ef41Sopenharmony_ci 1531cb0ef41Sopenharmony_ciIf you want to double check that your package will include only the files 1541cb0ef41Sopenharmony_ciyou intend it to when published, you can run the `npm pack` command locally 1551cb0ef41Sopenharmony_ciwhich will generate a tarball in the working directory, the same way it 1561cb0ef41Sopenharmony_cidoes for publishing. 1571cb0ef41Sopenharmony_ci 1581cb0ef41Sopenharmony_ci### Link Packages 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci`npm link` is designed to install a development package and see the 1611cb0ef41Sopenharmony_cichanges in real time without having to keep re-installing it. (You do 1621cb0ef41Sopenharmony_cineed to either re-link or `npm rebuild -g` to update compiled packages, 1631cb0ef41Sopenharmony_ciof course.) 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ciMore info at [`npm link`](/commands/npm-link). 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci### Before Publishing: Make Sure Your Package Installs and Works 1681cb0ef41Sopenharmony_ci 1691cb0ef41Sopenharmony_ci**This is important.** 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ciIf you can not install it locally, you'll have 1721cb0ef41Sopenharmony_ciproblems trying to publish it. Or, worse yet, you'll be able to 1731cb0ef41Sopenharmony_cipublish it, but you'll be publishing a broken or pointless package. 1741cb0ef41Sopenharmony_ciSo don't do that. 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ciIn the root of your package, do this: 1771cb0ef41Sopenharmony_ci 1781cb0ef41Sopenharmony_ci```bash 1791cb0ef41Sopenharmony_cinpm install . -g 1801cb0ef41Sopenharmony_ci``` 1811cb0ef41Sopenharmony_ci 1821cb0ef41Sopenharmony_ciThat'll show you that it's working. If you'd rather just create a symlink 1831cb0ef41Sopenharmony_cipackage that points to your working directory, then do this: 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci```bash 1861cb0ef41Sopenharmony_cinpm link 1871cb0ef41Sopenharmony_ci``` 1881cb0ef41Sopenharmony_ci 1891cb0ef41Sopenharmony_ciUse `npm ls -g` to see if it's there. 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ciTo test a local install, go into some other folder, and then do: 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ci```bash 1941cb0ef41Sopenharmony_cicd ../some-other-folder 1951cb0ef41Sopenharmony_cinpm install ../my-package 1961cb0ef41Sopenharmony_ci``` 1971cb0ef41Sopenharmony_ci 1981cb0ef41Sopenharmony_cito install it locally into the node_modules folder in that other place. 1991cb0ef41Sopenharmony_ci 2001cb0ef41Sopenharmony_ciThen go into the node-repl, and try using require("my-thing") to 2011cb0ef41Sopenharmony_cibring in your module's main module. 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci### Create a User Account 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ciCreate a user with the adduser command. It works like this: 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci```bash 2081cb0ef41Sopenharmony_cinpm adduser 2091cb0ef41Sopenharmony_ci``` 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ciand then follow the prompts. 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ciThis is documented better in [npm adduser](/commands/npm-adduser). 2141cb0ef41Sopenharmony_ci 2151cb0ef41Sopenharmony_ci### Publish your Package 2161cb0ef41Sopenharmony_ci 2171cb0ef41Sopenharmony_ciThis part's easy. In the root of your folder, do this: 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ci```bash 2201cb0ef41Sopenharmony_cinpm publish 2211cb0ef41Sopenharmony_ci``` 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ciYou can give publish a url to a tarball, or a filename of a tarball, 2241cb0ef41Sopenharmony_cior a path to a folder. 2251cb0ef41Sopenharmony_ci 2261cb0ef41Sopenharmony_ciNote that pretty much **everything in that folder will be exposed** 2271cb0ef41Sopenharmony_ciby default. So, if you have secret stuff in there, use a 2281cb0ef41Sopenharmony_ci`.npmignore` file to list out the globs to ignore, or publish 2291cb0ef41Sopenharmony_cifrom a fresh checkout. 2301cb0ef41Sopenharmony_ci 2311cb0ef41Sopenharmony_ci### Brag about it 2321cb0ef41Sopenharmony_ci 2331cb0ef41Sopenharmony_ciSend emails, write blogs, blab in IRC. 2341cb0ef41Sopenharmony_ci 2351cb0ef41Sopenharmony_ciTell the world how easy it is to install your program! 2361cb0ef41Sopenharmony_ci 2371cb0ef41Sopenharmony_ci### See also 2381cb0ef41Sopenharmony_ci 2391cb0ef41Sopenharmony_ci* [npm](/commands/npm) 2401cb0ef41Sopenharmony_ci* [npm init](/commands/npm-init) 2411cb0ef41Sopenharmony_ci* [package.json](/configuring-npm/package-json) 2421cb0ef41Sopenharmony_ci* [npm scripts](/using-npm/scripts) 2431cb0ef41Sopenharmony_ci* [npm publish](/commands/npm-publish) 2441cb0ef41Sopenharmony_ci* [npm adduser](/commands/npm-adduser) 2451cb0ef41Sopenharmony_ci* [npm registry](/using-npm/registry) 246