Basic First Tutorial on using Node Package Manager (npm)

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

After you have installed node.js, it comes with the node package manager (or more commonly known as npm).

The node package manager is a package installer that keeps tracks of package dependencies and will install or update whatever dependencies that are needed.  In this tutorial we will install coffee-script package …

1.  First I like to create a project folder to work in.   I created a new folder c:\tutorials\coffeescript-example

2. Open up the node.js command prompt as described in the previous tutorial and navigate to this folder.

3.  Confirm that you have npm by typing the following in the command prompt…

nodejs command prompt

nodejs command prompt

This will give you the version information of npm.  Here we are using npm version  1.3.11. Note that this is not the same as the node.js version.

4.  Typing …

npm –help

will provide you with some help with the command line arguments.  And typing …

npm -l

will give you full usage information.

5. You will see that “search” is one of the npm commands.   Let’s run …

npm search coffee-script

searching coffeescript

searching coffeescript

Note that coffee-script has hyphen.  We know that the package name is “coffee-script” because that was what was indicated in the install instructions in the official coffeescript.org site.

6. When you run search for the first time, it may take a while because it has to download the entire index from the npm registry at https://npmjs.org/ which you can search from as well (which sometimes can be a nicer way to search) …

npm registry

npm registry

7.  You can install a package globally or locally.  We will install coffee-script globally with the -g flag by…

npm install -g coffee-script

8. You can see what global packages are installed by typing …

npm ls -g

and what packages are locally installed in a directory by typing …

npm ls

The ls will show the dependency tree and also reveal the location where the package was installed.  In our case, it was installed in …

C:\Users\TutorialWriter\AppData\Roaming\npm\node_modules\coffee-script

In there you will find a package.json that lists any dependencies (which npm would also download for you).

9. Typing “npm update -g” will update global modules.

10.  To uninstall a global package, you would do …

npm uninstall -g coffee-script

It would uninstall that package and its dependencies (as long as its dependencies are not used by other packages).