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.

npm link with no arguments

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…

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.