Don’t sudo everything. Use this nice .bashrc aliasing.

This guide from sindresorhus is a brilliant workaround to many of the permissions errors you get when installing node packages with npm. Using sudo can be dangerous:

“It defeats the security model that’s been in place for years. Applications are meant to be run with non-administrative security (or as mere mortals) so you have to elevate their privileges to modify the underlying system. For example you wouldn’t want that recent crash of Rhythmbox to wipe out your entire /usr directory due to a bug. Or that vulnerability that was just posted in ProFTPD to allow an attacker to gain a ROOT shell.” –LazyPower

So don’t do it. I discovered this today while walking through the tutorial on Yeoman.

The walkthrough from sindresorhus:

npm installs packages locally within your projects by default. You can also install packages globally (e.g. npm install -g <package>) (useful for command-line apps). However the downside of this is that you need to be root (or use sudo) to be able to install globally.

Here is a way to install packages globally for a given user.

1. Create a directory for your global packages

mkdir "${HOME}/.npm-packages"

2. Reference this directory for future usage in your .bashrc/.zshrc:

NPM_PACKAGES="${HOME}/.npm-packages"

3. Indicate to npm where to store your globally installed package. In your $HOME/.npmrc file add:

prefix=${HOME}/.npm-packages

4. Ensure node will find them. Add the following to your .bashrc/.zshrc:

NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"

5. Ensure you’ll find installed binaries and man pages. Add the following to your .bashrc/.zshrc:

PATH="$NPM_PACKAGES/bin:$PATH"
# Unset manpath so we can inherit from /etc/manpath via the `manpath`
# command
unset MANPATH # delete if you already modified MANPATH elsewhere in your config
MANPATH="$NPM_PACKAGES/share/man:$(manpath)"

Check out npm-g_nosudo for doing the above steps automagically

NOTE: If you are running OS X, the .bashrc file may not yet exist, and the terminal will be obtaining its environment parameters from another file, such as .profile or .bash_profile. These files also reside in the user’s home folder. In this case, simply adding the following line to them will instruct Terminal to also load the .bashrc file:

source ~/.bashrc
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.