Book Image

Instant Node Package Manager

By : Juzer Ali
Book Image

Instant Node Package Manager

By: Juzer Ali

Overview of this book

Node.js is the new buzz in town and has a vibrant community.It ships with npm, which is a tool that helps automate various development, deployment, and installation of node modules, and makes the process much easier. Instant Node Package Module is a practical guide to authoring and distributing node modules, and also for creating and managing private, standalone node.js projects. Starting with getting node.js installed and running on a variety of platforms, we then proceed to learn about code modularization and achieving it in node.js. Picking up a practical example, we will then explore the whole process and life cycle of conceiving, writing, and publishing a node module. We will also learn how to exploit a toolsincluded with npm to automate various development and deployment steps. Later on, we will discover the power of npm and how its different features and configurations can save large amounts of time by providing a helping hand for automating repetitive tasks between writing and publishing your modules.
Table of Contents (8 chapters)


In this section, we will get node.js and npm installed and running on our machines. Earlier node.js and npm required separate installations. But in later versions npm made it to the core of node.js, and now comes bundled with it. We will use command line extensively throughout this book, including during installation. npm does not provide any tool with graphical user interface, so some familiarity with the command line is necessary to work with it.

Step 1 – what do I need?

Before beginning to install node.js, you will need to check that your system meets the following requirements:

  • Disk Space: 50 MB free (min). This much should suffice for all our needs of installing node.js and creating our own programs. However, as you begin using elaborate node.js packages and libraries, you might need more space to store them.

  • Memory: 256 MB (min); 1 GB (recommended).

  • Operating System: node.js supports a wide range of operating systems including Windows, Mac, Solaris, and Linux. It is unlikely that you will fail to install node.js due to an operating system.

Step 2 – downloading node.js

Download instructions vary for different platforms. We have provided instructions for three most popular platforms, Windows, Mac, and *nix flavored operating systems. The instructions for Linux hold for most of the derivatives and distributions. You need to download the respective files from node.js downloads page:

Windows or Mac

The easiest way to install node.js on Windows/Mac is through respective installers.

Step 1 – downloading Windows/MAC installer

Download the Windows installer .msi package from the download page ( If you are using Mac, download the Mac .pkg instead.

Step 2 – running the installer

Run the installer by double-clicking it and follow the instructions.

Step 3 – testing

Open the Windows command prompt or a terminal on Mac OS and provide the following instructions:

$ node -–version
$ npm –-version

Linux and other Unices

There are lots of ways of installing node on *nix flavored Operating Systems. The instructions given here will hold for most of them.

One step installation

There is a one-step installation process by which you can install node.js through command line. But that may not always work.

$ echo prefix = ~/local >> ~/.npmrc
$ sudo curl | sh
Foolproof installation

Following is a step-by-step guide for manual installation.

Step 1 – getting the binary

Go to and choose Linux Binaries. Choose a 32-bit or a 64-bit version according to your machine's CPU architecture. If you don't know what this means, choose 32-bit. A tar.gz file will be downloaded with a name similar to node-v0.10.5-linux-x86.tar.gz. The file name might differ depending upon the version of node, operating system, and your computer architecture. In general, the name will follow the pattern, node-v<version>-<OS>-<architecture>.tar.gz.


If you are using Solaris (SunOS) download SunOS binaries instead.

Step 2 – extracting the file

Extract the file either using your favorite GUI based extractor or using command line.

$ cd path/to/downloaded/tar-file
$ tar -xvf  node-v0.10.5-linux-x86.tar.gz

You should be left with a folder named node-v0.10.5-linux-x86.

Step 3 – testing
$ cd  node-v0.10.5-linux-x86/bin
$ ./node -–version && ./npm --version

This should print something like the following:

Step 4 – symlink

Binaries are runnable pieces of code that can be directly run from the command line. This package has given us exactly that. But to run them, we need to locate them on our machine. We don't want to remember where we have stored the binaries. That's why we will put them in a location which is present in our system's PATH variable so that they can be accessed from anywhere. To do that, we will symlink the binaries to: /usr/local/sbin.

$ sudo ln -s ./node /usr/local/sbin/node
$ sudo ln -s ./npm /usr/local/sbin/npm

If you do not have super user privilege on your machine you can add the node.js binary folder on your system's PATH variable instead:

$ export PATH=PATH:/path/to/nodejsfolder/node-v0.10.5-linux-x86/bin
Step 5 – testing symlink
$ cd to/a/random/location/on/your/computer
$ node –-version && npm –-version

Various Linux distributions provide node.js from their package distributions. You can install node.js from your respective package distribution. Specific instructions for most distros can be found at Usually these distros do not keep up with the node.js development and are several versions old.

Installing via nvm and nave (recommended)

On *nix systems, installing node directly as binaries doesn't help much in keeping the installation up to date. In fact, you will have to repeat the exact instructions again for the version you want to upgrade or downgrade to. node version manager (nvm) is a shell program that provides a convenient way of trying multiple node.js versions at the same time.

Switching between two versions is just a matter of running one shell command. It does this by creating a virtual environment. What this means is that nvm keeps binaries in a user's home directory. Whenever the user instructs it to change versions, nvm manipulates the system's PATH variable so that it exposes the binary requested.

Github URL for nvm is where you can find the instructions for installing and using nvm. In a nutshell, following sequence of instructions will install node v0.10.

wget -qO- | sh
source ~/.nvm/ # do it every time a new shell is started
nvm install 0.10
nvm use 0.10

You can install different versions of node by using nvm install command and switch between these versions by using nvm use command. Note that the second command, source ~/.nvm/ has to be run every time a new shell is started. To automate this step, add the command at the end of ~/.bashrc file.

A similar program called nave also exists which accomplishes the same purpose. It can be found at Nave also creates a virtual environment for testing out different versions of node. There are subtle differences between nvm and nave, the major one being that nvm downloads binaries directly, while nave compiles each version from source. Nave requires you to have a C++ compiler installed on your system.

You can go through the README of both the programs and choose for yourself the one that fits your needs. Generally any one of them should work for most purposes.

Installing node through nave or nvm is recommended since it will help you test your modules between different versions quickly.

The downside of using virtual environment is that it will be available only for the user who has installed it and not other users on the same system.

Building and installing from source

If you are interested in building and installing node.js from its source code, the detailed instructions are available on the wiki of node.js github repository. Here is the link:

And that's it!

We are now ready to explore node.js and build our very first module.