An npm package is a directory structure with a package.json file describing the package. This is exactly what was referred to earlier as a directory module, except that npm recognizes many more package.json tags than Node.js does. The starting point for npm's package.json are the CommonJS Packages/1.0 specification. The documentation for npm's package.json implementation is accessed using the following command:
$ npm help json
A basic package.json file is as follows:
{ "name": "packageName",
"version": "1.0",
"main": "mainModuleName",
"modules": {
"mod1": "lib/mod1",
"mod2": "lib/mod2"
}
}
The file is in JSON format, which, as a JavaScript programmer, you should be familiar with.
The most important tags are name and version. The name will appear in URLs and command names, so choose one that's safe for both. If you desire to publish a package in the public npm repository, it's helpful to check whether a particular name is already being used at http://npmjs.com or with the following command:
$ npm search packageName
The main tag is treated the same as we discussed in the previous section on directory modules. It defines which file module will be returned when invoking require('packageName'). Packages can contain many modules within themselves and they can be listed in the modules list.
Packages can be bundled as tar-gzip archives (tarballs), especially to send them over the internet.
A package can declare dependencies on other packages. That way, npm can automatically install other modules required by the module being installed. Dependencies are declared as follows:
"dependencies": {
"foo" : "1.0.0 - 2.x.x",
"bar" : ">=1.0.2 <2.1.2"
}
The description and keyword fields help people find the package when searching in an npm repository (https://www.npmjs.com/). Ownership of a package can be documented in the homepage, author, or contributors fields:
"description": "My wonderful package that walks dogs",
"homepage": "http://npm.dogs.org/dogwalker/",
"author": "dogwhisperer@dogs.org"
Some npm packages provide executable programs meant to be in the user's PATH. These are declared using the bin tag. It's a map of command names to the script that implements that command. The command scripts are installed into the directory containing the node executable using the name given:
bin: {
'nodeload.js': './nodeload.js',
'nl.js': './nl.js'
},
The directories tag describes the package directory structure. The lib directory is automatically scanned for modules to load. There are other directory tags for binaries, manuals, and documentation:
directories: { lib: './lib', bin: './bin' },
The script tags are script commands run at various events in the life cycle of the package. These events include install, activate, uninstall, update, and more. For more information about script commands, use the following command:
$ npm help scripts
We've already used the scripts feature when showing how to set up Babel. We'll use these later for automating the build, test, and execution processes.
This was only a taste of the npm package format; see the documentation (npm help json) for more.