I use more and more Node.js tools, mostly for my web development tasks: linters, code formatters, unit tests… Most of the time, you want these tools to be installed “globally” so that they’re in your $PATH, e.g.:

$ npm install jshint -g

… which usually results in this kind of error:

npm ERR! Error: EACCES, mkdir '/usr/lib/node_modules/jshint'
[…]
npm ERR! Please try running this command again as root/Administrator.

Being a naive Linux user, I’m not giving root/Administrator privileges to an install script that hasn’t been reviewed — call me a freak. And I’m quite surprised that most Node.js users just “sudo” it.

A common workaround is to “chgrp” /usr/lib and/or /usr/local so that modifying these directories doesn’t require sudo. I find that even worse.

Solution: npm prefix

(I’ll suppose you’re using OSX or Linux here. If anyone knows how this works on Windows, please leave a comment)

After a bit of ddg’ing, I’m happy to see there’s a clean solution:

$ echo prefix=${HOME}/.local >> ~/.npmrc

And voilà, npm will install everything in ~/.local/bin and ~/.local/lib:

$ npm install -g jshint
/home/kaze/.local/bin/jshint -> /home/kaze/.local/lib/node_modules/jshint/bin/jshint
jshint@2.5.10 /home/kaze/.local/lib/node_modules/jshint

Of course, this supposes that ~/.local/bin belongs to your $PATH. If not, just edit your ~/.profile to add the following line:

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

Source your ~/.profile, you’re done.