Node.js HOWTO: Install Node+NPM as user (not root) under Unix OSes

Posted 2011.08.27   Revised 2012.05.08

This was my first hurdle with Node.js, and it could've been a deal-breaker. You see, most Node.js libs/utils are designed to be installed using NPM (Node Package Manager), and installing them (and their dependencies) by hand is a pain in the rear ... but NPM wants to be installed and run as root. In other words, sudo npm install possible-trojan-horse. Screw that! One rogue package, and you're PWNED. Also, that won't work in a shared hosting environment, which has some big advantages over virtualization. I/O throughput, for instance.

Fortunately, there's a simple workaround. I'd like to see this become the default NPM setup.

2012.05.08 Update:

  • NPM has been included with Node.js for a few months now, so I removed the part about installing it separately. It's now VERY EASY to install it the right way from source (which I recommend).
  • If you use a binary installer/package, you can still run npm install -g without being root -- just follow these instructions and skip step 2.
  • On OSX, your mileage may vary. The Node/OSX situation changes from month to month. I'll take another look at it soon.
  • Unfortunately the Nodejs docs still says to do it the wrong way (using sudo). There are a bunch of open issues involving "sudo" and "root". I'll see what I can do.
  • Rant: the deeper I dig, it looks like OSX is a bad influence on Node, NPM, and developers in general. Ubuntu too. Node is useful all around, but it's primarily a server thing. Real servers run CentOS, RHEL, Debian, FreeBSD, maybe even Windows... not OSX/Ubuntu.

INSTALLING Node+NPM

This will install Nodejs under ~/.local/ instead of the default /usr/local/

  1. Add this to your ~/.npmrc (create the file if it doesn't exist already):

    root =    /home/YOUR-USERNAME/.local/lib/node_modules
    binroot = /home/YOUR-USERNAME/.local/bin
    manroot = /home/YOUR-USERNAME/.local/share/man
    
  2. Download the Nodejs source code from nodejs.org and install it under your ~/.local tree:

    tar xf node......
    cd node........
    ./configure --prefix=~/.local
    make
    make install
    
  3. Create ~/.node_modules symlink. (This directory will be automatically searched when you load modules using require "module" in scripts. I'm not sure why Node doesn't search ~/.local/lib/node_modules by default.)

    cd
    ln -s .local/lib/node_modules .node_modules
    
  4. Is ~/.local/bin in your path? Type

    which npm
    

    If it says ~/.local/bin/npm, you're done.

    Otherwise, do this...

    export PATH=$HOME/.local/bin:$PATH
    

    ...and add that line to your ~/.profile file, so it'll run every time you log in.

    (Note: Unixes are adopting on ~/.local as the standard per-user program/library location, but they're not all there yet.)

INSTALLING PACKAGES (quick summary for Node.js newbies)

Global package installation under ~/.local (for libraries you use a lot, and command-line tools like coffee-script and jade and jslint):

npm install -g PACKAGE [PACKAGE2 PACKAGE3 ...]

Local package installation under $PWD/node_modules (redundant, but that's the point -- different packages can use different versions of the same dependency):

npm install PACKAGE [PACKAGE2 PACKAGE3 ...]

To locally install packages specified in package.json, just run:

npm install

CREDITS

I distilled these instructions from this which I found thanks to a comment here.

A few weeks later, I stumbled across Dave Stevens' instructions for installing Node+NPM on Webfaction which were more straightforward.