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

Posted 2011.08.27   Revised 2015.07.21

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.

Notes - updated 2015.07.21

  • Unfortunately, 4 years later, the Nodejs/NPM docs STILL say to do it the wrong way (using sudo). This page gets a lot of traffic; some people want to do it right, but not the Nodejs politburo.
  • FWIW, I am NOT using Node in production, only for JS utils like JSHint and a few little homebrew tools.
  • Will the madness never end? I had to change "~" to "$HOME" because Nodejs switched to yet another trendy new build tool (gyp) that doesn't interpret "~" correctly.
  • OSX users will need a C/C++ build environment. If that's too inconvenient, stop reading; you aren't paranoid enough to worry about this (and yes, as a JS coder you should be extremely paranoid).


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

  1. Add this to your $HOME/.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 and install it under your $HOME/.local tree:

    (You could try using a binary installer instead, but that hasn't worked for me since ~ 0.8.x)

    tar xf node......
    cd node........
    ./configure --prefix=$HOME/.local
    make install
  3. Create $HOME/.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 $HOME/.local/lib/node_modules by default.)

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

    which npm

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

    Otherwise, do this...

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

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

    (Note: Unixes are adopting on $HOME/.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 $HOME/.local (for libraries you use a lot, and command-line tools like coffee-script jade jslint jshint uglifyjs):

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


I distilled these instructions from a Nodejs setup howto on Joyeur. There was also a (much better) shared-hosting setup howto by Dave Stevens. Those links are all dead now.