350 CHAPTER 8 Websites and services
"addition",
"subtraction",
"multiplication",
"division",
"fibonacci"
],
"author": "Glenn Johnson",
"license": "BSD"
}
You can provide test scripts in your package so users can install your package and run the
tests on their computers.
You can specify a git repository URL. Git is one of the best source control managers, and
if you’re not using any kind of source control, this might be a good time to learn about git at
http://git-scm.com/. If your package has dependencies, they would also be listed in the pack-
age.json file.
Publishing the package
You now have a package with a folder structure that looks like the following.
\packageName
\bin
main.js
\lib
advanced_math.js
call_counter.js
simple_math.js
package.json
README.md
Publishing a package makes the package available to all users for use in their applications.
You use npm to publish to the npm registry, and then you use npm to search for packages
and install your package.
Before you can publish a node.js package, you must create an account by which to publish
all your packages. Use npm to create the account by executing the following command.
npm adduser
Follow the prompts for a user name (lowercase), password, and email address. After you’ve
entered all the required information, npm contacts registry.npmjs.org to create your account.
Validation of account currently is not required, so anyone can create an account and put bad
code into the registry; therefore, when you install an unknown package from the npm regis-
try, beware!
Now that you have an account, you can publish to the npm registry. Open the command
prompt window and change from your current directory to the root directory of the package,
where the package.json file is located. Enter the following command to publish the package.
npm publish