| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 | .TH "DEVELOPERS" "7" "August 2021" "" "".SH "NAME"\fBdevelopers\fR \- Developer Guide.SS Description.PSo, you've decided to use npm to develop (and maybe publish/deploy)your project\..PFantastic!.PThere are a few things that you need to do above the simple stepsthat your users will do to install your program\..SS About These Documents.PThese are man pages\.  If you install npm, you should be able tothen do \fBman npm\-thing\fP to get the documentation on a particulartopic, or \fBnpm help thing\fP to see the same information\..SS What is a package.PA package is:.RS 0.IP \(bu 2a) a folder containing a program described by a package\.json file.IP \(bu 2b) a gzipped tarball containing (a).IP \(bu 2c) a url that resolves to (b).IP \(bu 2d) a \fB<name>@<version>\fP that is published on the registry with (c).IP \(bu 2e) a \fB<name>@<tag>\fP that points to (d).IP \(bu 2f) a \fB<name>\fP that has a "latest" tag satisfying (e).IP \(bu 2g) a \fBgit\fP url that, when cloned, results in (a)\..RE.PEven if you never publish your package, you can still get a lot ofbenefits of using npm if you just want to write a node program (a), andperhaps if you also want to be able to easily install it elsewhereafter packing it up into a tarball (b)\..PGit urls can be of the form:.P.RS 2.nfgit://github\.com/user/project\.git#commit\-ishgit+ssh://user@hostname:project\.git#commit\-ishgit+http://user@hostname/project/blah\.git#commit\-ishgit+https://user@hostname/project/blah\.git#commit\-ish.fi.RE.PThe \fBcommit\-ish\fP can be any tag, sha, or branch which can be supplied asan argument to \fBgit checkout\fP\|\.  The default is \fBmaster\fP\|\..SS The package\.json File.PYou need to have a \fBpackage\.json\fP file in the root of your project to domuch of anything with npm\.  That is basically the whole interface\..PSee npm help \fBpackage\.json\fP for details about what goes in that file\.  At the veryleast, you need:.RS 0.IP \(bu 2name:This should be a string that identifies your project\.  Please do notuse the name to specify that it runs on node, or is in JavaScript\.You can use the "engines" field to explicitly state the versions ofnode (or whatever else) that your program requires, and it's prettywell assumed that it's JavaScript\.It does not necessarily need to match your github repository name\.So, \fBnode\-foo\fP and \fBbar\-js\fP are bad names\.  \fBfoo\fP or \fBbar\fP are better\..IP \(bu 2version:A semver\-compatible version\..IP \(bu 2engines:Specify the versions of node (or whatever else) that your programruns on\.  The node API changes a lot, and there may be bugs or newfunctionality that you depend on\.  Be explicit\..IP \(bu 2author:Take some credit\..IP \(bu 2scripts:If you have a special compilation or installation script, then youshould put it in the \fBscripts\fP object\.  You should definitely have atleast a basic smoke\-test command as the "scripts\.test" field\.See npm help scripts\..IP \(bu 2main:If you have a single module that serves as the entry point to yourprogram (like what the "foo" package gives you at require("foo")),then you need to specify that in the "main" field\..IP \(bu 2directories:This is an object mapping names to folders\.  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\..RE.PYou can use \fBnpm init\fP in the root of your package in order to get youstarted with a pretty basic package\.json file\.  See npm help \fBinit\fP formore info\..SS Keeping files \fIout\fR of your package.PUse a \fB\|\.npmignore\fP file to keep stuff out of your package\.  If there'sno \fB\|\.npmignore\fP file, but there \fIis\fR a \fB\|\.gitignore\fP file, then npm willignore the stuff matched by the \fB\|\.gitignore\fP file\.  If you \fIwant\fR toinclude something that is excluded by your \fB\|\.gitignore\fP file, you cancreate an empty \fB\|\.npmignore\fP file to override it\. Like \fBgit\fP, \fBnpm\fP looksfor \fB\|\.npmignore\fP and \fB\|\.gitignore\fP files in all subdirectories of yourpackage, not only the root directory\..P\fB\|\.npmignore\fP files follow the same pattern rules \fIhttps://git\-scm\.com/book/en/v2/Git\-Basics\-Recording\-Changes\-to\-the\-Repository#Ignoring\-Files\fRas \fB\|\.gitignore\fP files:.RS 0.IP \(bu 2Blank lines or lines starting with \fB#\fP are ignored\..IP \(bu 2Standard glob patterns work\..IP \(bu 2You can end patterns with a forward slash \fB/\fP to specify a directory\..IP \(bu 2You can negate a pattern by starting it with an exclamation point \fB!\fP\|\..RE.PBy default, the following paths and files are ignored, so there's noneed to add them to \fB\|\.npmignore\fP explicitly:.RS 0.IP \(bu 2\fB\|\.*\.swp\fP.IP \(bu 2\fB\|\._*\fP.IP \(bu 2\fB\|\.DS_Store\fP.IP \(bu 2\fB\|\.git\fP.IP \(bu 2\fB\|\.hg\fP.IP \(bu 2\fB\|\.npmrc\fP.IP \(bu 2\fB\|\.lock\-wscript\fP.IP \(bu 2\fB\|\.svn\fP.IP \(bu 2\fB\|\.wafpickle\-*\fP.IP \(bu 2\fBconfig\.gypi\fP.IP \(bu 2\fBCVS\fP.IP \(bu 2\fBnpm\-debug\.log\fP.RE.PAdditionally, everything in \fBnode_modules\fP is ignored, except forbundled dependencies\. npm automatically handles this for you, so don'tbother adding \fBnode_modules\fP to \fB\|\.npmignore\fP\|\..PThe following paths and files are never ignored, so adding them to\fB\|\.npmignore\fP is pointless:.RS 0.IP \(bu 2\fBpackage\.json\fP.IP \(bu 2\fBREADME\fP (and its variants).IP \(bu 2\fBCHANGELOG\fP (and its variants).IP \(bu 2\fBLICENSE\fP / \fBLICENCE\fP.RE.PIf, given the structure of your project, you find \fB\|\.npmignore\fP to be amaintenance headache, you might instead try populating the \fBfiles\fPproperty of \fBpackage\.json\fP, which is an array of file or directory namesthat should be included in your package\. Sometimes a whitelist is easierto manage than a blacklist\..SS Testing whether your \fB\|\.npmignore\fP or \fBfiles\fP config works.PIf you want to double check that your package will include only the filesyou intend it to when published, you can run the \fBnpm pack\fP command locallywhich will generate a tarball in the working directory, the same way itdoes for publishing\..SS Link Packages.P\fBnpm link\fP is designed to install a development package and see thechanges in real time without having to keep re\-installing it\.  (You doneed to either re\-link or \fBnpm rebuild \-g\fP to update compiled packages,of course\.).PMore info at npm help \fBlink\fP\|\..SS Before Publishing: Make Sure Your Package Installs and Works.P\fBThis is important\.\fR.PIf you can not install it locally, you'll haveproblems trying to publish it\.  Or, worse yet, you'll be able topublish it, but you'll be publishing a broken or pointless package\.So don't do that\..PIn the root of your package, do this:.P.RS 2.nfnpm install \. \-g.fi.RE.PThat'll show you that it's working\.  If you'd rather just create a symlinkpackage that points to your working directory, then do this:.P.RS 2.nfnpm link.fi.RE.PUse \fBnpm ls \-g\fP to see if it's there\..PTo test a local install, go into some other folder, and then do:.P.RS 2.nfcd \.\./some\-other\-foldernpm install \.\./my\-package.fi.RE.Pto install it locally into the node_modules folder in that other place\..PThen go into the node\-repl, and try using require("my\-thing") tobring in your module's main module\..SS Create a User Account.PCreate a user with the adduser command\.  It works like this:.P.RS 2.nfnpm adduser.fi.RE.Pand then follow the prompts\..PThis is documented better in npm help adduser\..SS Publish your package.PThis part's easy\.  In the root of your folder, do this:.P.RS 2.nfnpm publish.fi.RE.PYou can give publish a url to a tarball, or a filename of a tarball,or a path to a folder\..PNote that pretty much \fBeverything in that folder will be exposed\fRby default\.  So, if you have secret stuff in there, use a\fB\|\.npmignore\fP file to list out the globs to ignore, or publishfrom a fresh checkout\..SS Brag about it.PSend emails, write blogs, blab in IRC\..PTell the world how easy it is to install your program!.SS See also.RS 0.IP \(bu 2npm help npm.IP \(bu 2npm help init.IP \(bu 2npm help package\.json.IP \(bu 2npm help scripts.IP \(bu 2npm help publish.IP \(bu 2npm help adduser.IP \(bu 2npm help registry.RE
 |