I realize that this is an old thread, but for Future-Me, and posterity, I figured I should add my two-cents around the "running npm as sudo" discussion. Disclaimer: I do not use Windows. These steps have only been proven on non-windows machines, both virtual and physical.
You can avoid the need to use sudo by changing the permission to npm's default directory.
How to: change permissions in order to run npm without sudo
Step 1: Find out where npm's default directory is.
- To do this, open your terminal and run:
npm config get prefix
Step 2: Proceed, based on the output of that command:
- Scenario One: npm's default directory is
/usr/local
For most users, your output will show that npm's default directory is /usr/local, in which case you can skip to step 4 to update the permissions for the directory.
- Scenario Two: npm's default directory is
/usr
or /Users/YOURUSERNAME/node_modules
or /Something/Else/FishyLooking
If you find that npm's default directory is not /usr/local, but is instead something you can't explain or looks fishy, you should go to step 3 to change the default directory for npm, or you risk messing up your permissions on a much larger scale.
Step 3: Change npm's default directory:
- There are a couple of ways to go about this, including creating a directory specifically for global installations and then adding that directory to your $PATH, but since /usr/local is probably already in your path, I think it's simpler to just change npm's default directory to that. Like so:
npm config set prefix /usr/local
- For more info on the other approaches I mentioned, see the npm docs here.
Step 4: Update the permissions on npm's default directory:
- Once you've verified that npm's default directory is in a sensible location, you can update the permissions on it using the command:
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Now you should be able to run npm
without sudo
. Note: You may need to restart your terminal in order for these changes to take effect.