How to Avoid Sudo Npm

Comments

You know the story. You try to do npm install hipster-package on OS X and you get a whole load of errors; the easy way out is to just use sudo npm install. Everyone, however, keep saying how that is bad idea. But how do you make npm work without sudo? It’s quite simple actually.

On your dev machine

If you are the typical js developer who works on OS X, all you need to do is just change the owner of the node binaries

1
2
3
4
5
# this should solve the problem
sudo chown -R $USER /usr/local

# this may also help
sudo chmod -R 777 ~/.npm

If you are working on projects and someone has already checked in a package.json run with sudo, you need to make sure the whole team changes the permissions of at the same time

1
2
# is someone has checked in a version of package.json with the wrong permissions
chmod -775 package.json

Unix purists will find this hacky; and rightly so. The proper way of doing is to create a group for everyone who should be allowed to run npm, even if it’s just you. However for a simple dev machine that is good enough.

Comments