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.
- 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 -gwithout 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.
This will install Nodejs under ~/.local/ instead of the default /usr/local/
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
Download the Nodejs source code from nodejs.org and install it under your
tar xf node...... cd node........ ./configure --prefix=~/.local make make install
~/.node_modulessymlink. (This directory will be automatically searched when you load modules using
require "module"in scripts. I'm not sure why Node doesn't search
cd ln -s .local/lib/node_modules .node_modules
~/.local/binin your path? Type
If it says
~/.local/bin/npm, you're done.
Otherwise, do this...
...and add that line to your
~/.profilefile, so it'll run every time you log in.
(Note: Unixes are adopting on
~/.localas 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:
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.