article

Node.js ABC’s - N is for npm

Whether you are ready to start coding your first Node.js project, or if you are a seasoned veteran and need finish up a new exciting project, odds are you will need some functionality that has been written many times before by others. One of the beauties of the internet is the ability to create and share things. Node.js is a great language set in itself, but one key selling point is the vast amount of code that is available for free download.

The Node Package Manager, also known as npm, is a software system that automates the process of installing, upgrading, configuring, and removing Node.js packages. As of Node.js version 0.6.3, npm is bundled and installed with the environment so once you have access to the node command line tools, you also have access to the vast number of shared packages available to you.

As of this writing, there are over 180,000 packages available in the npm. You can find anything from credit card validators, to full language parsers. If you have a task to perform, odds are there is a 3rd party package to help you along. And the best feature I see is that packages are distributed as source script so you have full access to modify or extend the packages you pull from the repository.

In my article on Loading, I discuss how you use the "require" command to load a module

Installing NPM

As I mentioned above, npm is included with Node.js versions 0.6.3 and above. If you are using an older version of node, I'll leave it as an exercise to the reader to find the distribution package. Better yet, I'd suggest upgrading to a more recent version of Node.js!

npm is itself distributed as a package through npm so you can have npm upgrade itself. npm is updated quite frequently, so it's a good idea to upgrade npm once in a while

$ sudo npm install npm -g

Installing a package

Node.js has the concept of "local" and "global" packages. Local packages are installed as part of your current project and are not available to other projects in your system. Global packages are installed at the system level, so distributions that include command line tools (like npm) that you will want to use "globally", then you will want to include it in global scope.

Local Package Installation

To install a package locally, you'll use the "install" npm sub command. Most often you will just need to specify the package name as an argument.

$ npm install package-name

Global Package Installation

To install a package globally, you'll use the "-g" argument. Most often you will need to run this under "sudo" to allow for permissions in system folders.

$ sudo npm install -g package-name

Updating Packages

Since packages can be installed locally or globally, there is an update procedure for each as well. Local updates are the easiest and can be completed with the "update" npm command. This command will update all the packages in the local installation.

$npm update

To update packages globally, you add the "-g" option like you did above.

$npm update -g

But, in some cases you won't want to update all global packages at once. You can use the "outdated" command to get a listing of global packages that need updating:

$ npm outdated -g --depth=0

and then you can update the global package with the same install command you used above

$ npm install -g package-name

Removing Packages

If the package you recently downloaded isn't living up to your expectations, you can use the "uninstall" npm command to remove the package from a local or global installation. For a global installation, use the "-g" option as above

$ npm uninstall package-name
$ npm uninstall -g package-name

Creating Packages

In my article on Node.js Modules, I go over in detail the package creation process. I'd suggest you read that article for details on creating your own package.

Publishing Packages

Once you have a great package put together, you may want to share that with the world. The process for doing so is very easy. You must first have a npm user account. You can create a new user with the "npm adduser" command, or alternately login if you have an existing account with the "npm login" command.

Once you are authenticated, you can use the "publish" npm command to push your package to the npm repository. use the folder name of your package in the "my-cool-package" parameter below: