Install NPM packages globally without sudo

To be an effective full-stack Javascript developer, you pretty much have to use npm (Node Packaged Modules)Npm-logo.svg

npm is the official package manager for Node.js. Using it allows you to easily install packages such as underscore, express, grunt, gulp, socket.io from the command line. (read more about npm on Wikipedia and npmjs.org).

Many of these packages need to be installed globally to be used, like so:

$ npm install -g grunt-cli

The -g denotes a global install.

But, in some environments, attempting to install globally without including sudo might give you this sort of error message:

npm ERR! Error: EACCES, open '/Users/YourNameHere/.npm/-/all/.cache.json'
 npm ERR! { [Error: EACCES, open '/Users/YourNameHere/.npm/-/all/.cache.json']
 npm ERR! errno: 3,
 npm ERR! code: 'EACCES',
 npm ERR! path: '/Users/YourNameHere/.npm/-/all/.cache.json' }
 npm ERR!
 npm ERR! Please try running this command again as root/Administrator.

Should you just do what the error message says and run this command as root using sudo? The short answer is: NO! Packages can run their own scripts, which makes installing them with sudo about as safe as shaving with a blowtorch.

There are many ways to solve this problem, but the easiest I’ve found is to simply change what folder npm installs packages into. Just use this terminal command:

npm config set prefix ~/npm

This tells npm to install packages into your home directory in an “npm” subfolder. It will automatically create this new subfolder the first time you globally install a package.

But we’re not done yet! You also need to modify your $PATH to include this new folder. To do this, open the config file of your shell in your favorite text editor. This is most likely “.bashrc” in your home directory, or “.zshrc” if you use Z shell. Once you have the file open, search for a line that looks like this:

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

At the end of this line, add:

 ":$HOME/npm/bin".

After I made the change, my path line looked like this:

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:$HOME/npm/bin"

The $HOME part of that just tells the shell to look in your home directory for a folder called “npm”, and then a folder inside that called “bin”. Once you’ve done these two things, you should now be able to install packages globally in npm without using sudo. Enjoy the tremendous feeling of freedom this brings.

3 thoughts on “Install NPM packages globally without sudo”

  1. Hi, I’m trying to set a $PATH but I’m having trouble locating my shell config file to open up in sublime to add the required export.. Any help would be much appreciated. I already have the npm sub folder as I have installed Bower -g..

      1. Hi Ben, the config files don’t have an extension in the sense that Windows files generally do. The file will likely be called either “.bashrc” or “.zshrc”, and will be located in your home directory (/home/yourusername in most Linux filesystems). If you can’t find the file, you may need to reveal hidden files. Try searching for an option like “Show Hidden Files” in your OS’s file manager, or in terminal type “ls -a” while in your home directory to see if it shows up. Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *