dribbblefacebookgithubgooglepluslinkedinrsstwitter

Global npm Install without Sudo

Posted on by Devin Clark

You should not have install global node modules with root! The default for directory global node modules is usually /usr/local/ when installing from the http://npmjs.org binary. This directory is not writable by users (as intended). I would like to preface this with I have only done this on a OS X. The instructions should be the same for Linux. Sorry Windows users, I can't help here.

The secret to fixing this problem is to change the directory npm uses to globally install. To do this we will change the prefix option in npm config. First, we can run npm config get prefix to see where packages are currently being installed. Next, make a directory where you would like the packages to be installed (and is user writeable). I use ~/npm.

Next, we need to tell npm to use the newly created directory by running npm config set prefix ~/npm.

You might think we are done now but now we will need to add ~/npm/bin to $PATH so we can use the executables. I store $PATH changes in ~/.bashrc. If you would like to store them in another file, feel free.

<troll type="fanboy">For speed I prefer to use the best command line text editor, nano. </troll> Anyways, just add this to the file then restart your Terminal window.

PATH=$PATH:$HOME/npm/bin

That's it! You will need to reinstall any global modules you had previously installed.

Also, my boss, Jordan uses this setup with nvm. He had a problem because nvm installs global packages in versioned directories. This would break all his npm link's on version updates. npm link is a big part of our Node development workflow.

More Information