10 May

Taking Baby Steps with Node.js – npm 1.0

Category:UncategorizedTag: :

< The list of previous installments can be found here. >

Isaac recently released version 1.0 of npm, which is a package manager for Node.js. I?ve been using npm very early on when I started fiddling with Node.js and I?ve come to rely on it quite heavily. After I upgraded to one of the release candidates, I noticed a big improvement in how and where packages are being installed.

With the old version that I was using (0.3.15), all packages where installed globally. You could install multiple versions of the same package, but this implied that we also had to take this into account when we loaded a particular module in our application:

var step = require([email protected]?);

Although this worked great, it?s still my humble opinion that all third-party modules should be right along the source code of the application that I?m building. With the npm 1.0 release, this has now become the default behavior.

Installing npm 1.0 is as simple as executing the following in the command-line:

curl http://npmjs.org/install.sh | sh

Executing this command asks the user whether any old versions of npm should be removed. You can also execute the following if you don?t like to be prompted:

curl http://npmjs.org/install.sh | clean=yes sh

In a previous post, I discussed a new convention introduced in version 4.0.x of Node.js for loading third-party modules from a folder named node_modules that is typically created in the root folder of a project. When installing a package, npm adheres to this convention by locally installing this package into this node_modules folder.

Say, for example, that we want to use Socket.IO in our application. We just need to run the following command from the root folder of our application:

npm install socket.io

This will locally install the requested third-party module into ./node_modules/socket.io. If a particular package includes a binary, then these will go into ./node_modules/.bin/. Now we can simply load the installed module without specifying a specific version number:

var socketIO = require(‘socket.io’);

So what do we do if we want to install packages that are typically used from the shell and not by the source code of the application, like n, node-inspector or nodemon? Well, we can still get them installed globally using the ?g command-line switch:

npm install -g n
npm install -g node-inspector
npm install -g nodemon

This ability to install packages either locally or globally is probably the most obvious new feature that has been shipped with the 1.0 release. There are several other new features and enhancements as well, but I haven?t ran into those (yet).

Apparently, there will also be no new major features or architectural changes for quite some time. So this gives us plenty of time to get to the bottom of all the new capabilities of npm. This local/global installation of packages alone is certainly worth upgrading to the 1.0 release.

Until next time.

One thought on “Taking Baby Steps with Node.js – npm 1.0”

  1. Thanks very much. I used earlier version of  npm and when I changed to npm v1.0 I got socked.. ur explanation is much clearer thanks 🙂

Comments are closed.