node.js notes

node.js is the lastest and hyped-as-greatest web development platform.


  • Supervisor for auto-restarting the web server when source changes are made.


npm is node’s package manager (bizarrely it’s not an acronym for Node Package Manager, it’s actually a recursive bacronymic abbreviation for “npm is not an acronym” (as explained in their faq).

Installing module specified in package.json

npm install

This will place the module in your application’s node_modules directory (and create it if it doesn’t already exist).

Installing specific module

npm install [module]

Installing modules globally

Note: I tend not do this as I find the behaviour of npm link confusing (see the oddities section below).

The differences between local and global installation are described here: npm 1.0: Global vs Local installation. From that page…

globally —- This drops modules in {prefix}/lib/node_modules, and puts executable files in {prefix}/bin, where {prefix} is usually something like /usr/local. It also installs man pages in {prefix}/share/man, if they’re supplied.
locally —- This installs your package in the current working directory. Node modules go in ./node_modules, executables go in ./node_modules/.bin/, and man pages aren’t installed at all.

Use the -g (or –global) flag:

sudo npm install -g
sudo npm install [module] -g

(You will need root privileges for this, so use sudo)

If you don’t specify a module then npm will create /usr/lib/node_modules/[app], where [app] is the name of your application as specified in its package.json file, with the modules in /usr/lib/node_modules/[app]/node_modules.

If you specify a module it’ll create /usr/lib/node_modules/[module]

Linking to global modules

When you install a module globally, which requires root permissions, you will need to link to it from your application (otherwise you’ll get an “Error: Cannot find module ‘foo’).

npm link [module]

Oddly, if you run this as a normal user you’ll get a permissions error e.g. [Error: EACCES, mkdir '/usr/lib/node_modules/express']:

Run the command via sudo (hence with root permissions) and it’ll work and create you a symlink in [app]/node_modules/ pointing to the global module. This however creates the symlink (and may create the node_modules directory) as owned by root, so you can no longer manage those files as a normal user!

If you initially installed the global modules with sudo npm install -g without specifying the module names (so you have /usr/lib/node_modules/[app]) and you then run sudo npm link [module], then the module will be created as /usr/lib/node_modules/[module] and a symlink created in your app (as above). You’ll then have both /usr/lib/node_modules/[app] and /usr/lib/node_modules/[module]. Again, the link will be owed by root.

If you don’t specify a module name then it’ll move the modules from /usr/lib/node_modules/[app]/node_modules to your application’s node_modules folder and create a symlink from /usr/lib/node_modules/[app] pointing to your [app]. Again, it creates these as owned by root so you have no control over them as your normal user.

Fetch failed

If you get a ‘fetch failed’ when attempting to install a package via npm, it could be that you’re using an old registry or it could be that npm have changed how package URLs are resolved. It was the latter for me, so I waited a day and it was fixed. Details follow…


When I first attempted to use npm I received the following errors:

bpc node-test # npm install express -g
npm http GET
npm http 304
npm http GET
npm http 404
npm ERR! fetch failed
npm ERR! Error: 404 Not Found
npm ERR!     at null.<anonymous> (/usr/lib64/node_modules/npm/lib/utils/fetch.js:47:16)
npm ERR!     at EventEmitter.emit (events.js:115:20)
npm ERR!     at WriteStream.flush (fs.js:1511:12)
npm ERR!     at fs.close (/usr/lib64/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:94:5)
npm ERR!     at Object.oncomplete (fs.js:297:15)
npm ERR!     at process.startup.processMakeCallback.process._makeCallback (node.js:238:20)
npm ERR! If you need help, you may report this log at:
npm ERR!     <>
npm ERR! or email it to:
npm ERR!     <>

npm ERR! System Linux 3.2.12-gentoo
npm ERR! command "node" "/usr/bin/npm" "install" "express" "-g"
npm ERR! cwd /home/steph/webapps/node-test
npm ERR! node -v v0.9.0
npm ERR! npm -v 1.1.44
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/steph/webapps/node-test/npm-debug.log
npm ERR! not ok code 0

Attempting to retrieve the express archive via a browser verified that the registry was down, with a security certificate exception followed by a 404 not found (with JSON {"error":"not_found","reason":"document not found"}).

A search revealed a number of issues in their bug trackers about 404s and failed fetches (e.g. npm install => http error 404). It seems that npm isn’t distributed and doesn’t have official mirrors (yet) so there is a single point of failure - if the registry goes down you can’t install packages. Not good.

A workaround posted on that GitHub issue (and on this StackOverflow post Can’t install any packages in Node.js using “npm install”) suggests that the npm folks have changed how the package URLs are specified: ->

So I could install packages by giving their full URL as an argument to npm install, but I’d have to do this for all dependencies as well.

However, the core problem is that the npm registry is resolving package names to incorrect URLs, so I’m going to wait until they’ve fixed the problem (I have that luxury at the mo).


If you get the following error:

TypeError: Cannot read property 'connect.sid' of undefined

Add app.use(express.cookieParser()) before you use express.session.


Last modified: 12/08/2012 Tags: ,

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top