Node Tutorial Part 23: npm 1.0

2011-05-02 00:00:00 +0100 by Alex R. Young

Welcome to part 23 of Let's Make a Web App, a tutorial series about
building a web app with Node. This series will
walk you through the major areas you'll need to face when building your
own applications. These tutorials are tagged with

Click to show previous tutorials.

npm 1.0

I upgraded to npm 1.0.3 recently. It generated a list of packages that
were incompatible, I haven't bothered reinstalling them yet.

Isaac's installation instructions seem to have solidified at:

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

... but read the npm README before doing anything!

Changes in Nodepad

It takes a while to get used to npm 1.x, but I like the changes. A big
change is the default installation path: running npm install
will install package in the local
./node_modules folder.

What this means for Nodepad is we no-longer need the
require('package@version') syntax.

I've updated Nodepad to work with npm 1.0: commit

npm install

From Nodepad's directory, running npm install will build
and install the dependencies to ./node_modules. That means
it's completely self-contained from everything else on your system.
Running npm g install will install all of the
dependencies in NODE_PATH/nodepad/ - again, making the
dependencies self-contained.

Using Nodepad is now fairly simple:

$ git clone git://github.com/alexyoung/nodepad.git
$ cd nodepad
$ npm install
$ mongod
$ node app.js

This is very similar to how npm bundle used to work. When
using Express apps written by other people, their documentation may
suggest running npm bundle, but npm install
should work.

Search vs. List

The npm ls command used to search available packages. It
now lists local packages:

$ npm ls -g
├─┬ express@2.3.2
│ ├── connect@1.4.0
│ ├── mime@1.2.1
│ └── qs@0.1.0
├── highlight@0.1.0
├── jade@0.10.6
├── markdown@0.2.1
├── n@0.4.1

Using npm search will search remote packages:

$ npm search nodepad
nodepad  A notepad written with Node  =alexyoung

Why the Change?

Isaac has been blogging and discussing npm 1.0 for some time on the
Node blog and npm discussion group. A major contributing
factor is Node's 0.4 changes to the module loading system. These changes
were mentioned in the Node 0.4

require() now has a primitive understanding of package.json. It looks for the main script. This allows require() to work on package directories directly.


A specially named directory, node_modules/, is searched in the current directory for any modules. This will hopefully encourage programmers to bundle modules rather than rely on a global namespace.

It might feel awkward to have to adapt existing applications to work
with npm 1.0, but the changes should be minimal and should make module
management easier for everyone.