How to run npm without sudo


I have written a new post explaining how to do this better and easier using nvm

The problem

A lot of people (myself included) have run into permissions errors installing packages globally via npm.

The common reaction (I did it!) is to simply sudo npm install *** which works, but will only cause more problems down the line!

I have read a lot of discussions online, and it seems that this is usually caused by installing node using sudo (often required when using package managers), or by using sudo to install the first package.
This causes the ~/.npm cache folder with root permissions which means that all subsequent installs also require root permissions.

Some people suggest re-installing node from source to get round this, but this still requires some further configuration so I prefer to install via my package manager of choice and just reconfigure npm to work without sudo.
This solution works fine for people who already have node & npm installed – you just need to reinstall your global packages at the end.

I have tested this in OS X (Yosemite), Ubuntu 14.04 and a number of older versions of Linux Mint.

What are you going to need to reinstall?

Before you begin, I suggest you get a list of your globally installed packages so you know what to reinstall later:

npm list -g --depth=0

Sample output:

├── bower@1.3.12
├── grunt-cli@0.1.13
├── gulp@3.8.10
├── npm@1.4.28
└── yo@1.3.3

Tell npm to install packages to home

By default, npm installs packages under /usr/lib/node_modules or /usr/local/lib/node_modules depending on your OS / distribution.
If you’re curious, you can check like this:

npm config get prefix

Your install path is the output of the above command plus /node_modules.
So if the command outputs /usr/lib then your install path is /usr/lib/node_modules.

The first step is to get it to install to your home directory instead.

npm config set prefix ~/npm

Fix your paths

Next, you need to add the appropriate paths to your environment variables…

# open your .bashrc (Linux) or .bash_profile (Mac) file for editing:
nano ~/.bashrc # for Linux
# or...
nano ~/.bash_profile # for Mac if you haven't created a .bashrc file

# add these lines:
export PATH="$PATH:$HOME/npm/bin"
export NODE_PATH="$NODE_PATH:$HOME/npm/lib/node_modules"

# save the file and then enter this command to make the changes take effect:
. ~/.bashrc
# or...
. ~/.bash_profile

Take ownership

Now you need to take ownership of everything that has previously been installed in ~/.npm.
Anything that was previously installed using sudo npm install will be owned by root and will cause problems later.

# optionally check to see if anything is owned by root:
ls -la ~/.npm

# take ownership of everything in your ~/.npm directory (must be run with sudo):
sudo chown -R $USER:`id -g -n $USER` ~/.npm

Reinstall your global packages

Finally, reinstall all your global packages (the ones you checked for in the first step)… this time without sudo!

# no sudo necessary 🙂
npm install -g bower
npm install -g grunt-cli
npm install -g gulp
npm install -g yo

And that’s it – hopefully this helps someone.

If I missed anything, or if there is a better way, please let me know in the comments.

Share your thoughts

Leave a Reply


I worked around this by installing npm & node using NVM. The upside is that you can switch between versions of node. Essential when writing modules that need to be tested on different versions locally. Have a look at :

Ahmed Ghanim

Thanks you ... you save me :)

Warren Cromartie

Fantastic write up, Thanks!


Great Stuff Bro :D


I like like approach, and it worked like a charm for me. Thank you! Perhaps add a step at the end to purge the node modules at the old location?


Wooooow!! Hands up!!

Felix Golindano

Hi! Thanks for help! In the last step, terminal is giving me an error that says: staff~/.npm: No such file or directory. What can I do?


This is the first tutorial I've found that actually worked and wasn't unnecessarily complicated. Future readers: I'm running OSX Yosemite 10.10.5


It's like people can't think beyond the nose on their face them "fix it by changing the prefix" me "what about all the stuff that's already installed?" CRICKETS. Thank you for a step by step to an actual solution.


As time goes on, I'm finding that there's things missing after having done this. The real problem is node/npm and it's installation. Simply put, a person shouldn't be moving these things around. Of course if node/npm didn't shit the bed in the first place, we wouldn't be having this conversation. Since I'm just developing, i'll just deal with sudo and leave deployment to the experts.


Really helps a lot! THX~


I've tried all this tutorial and I still getting this problem: $ ionic platform add android Updated the hooks directory to have execute permissions /usr/lib/node_modules/cordova/node_modules/update-notifier/node_modules/configstore/index.js:53 throw err; ^ Error: EACCES: permission denied, open '/home/henrique/.config/configstore/update-notifier-cordova.json' You don't have access to this file. at Error (native) at Object.fs.openSync (fs.js:549:18) at Object.fs.readFileSync (fs.js:397:15) at Object.create.all.get (/usr/lib/node_modules/cordova/node_modules/update-notifier/node_modules/configstore/index.js:34:26) at Object.Configstore (/usr/lib/node_modules/cordova/node_modules/update-notifier/node_modules/configstore/index.js:27:44) at new UpdateNotifier (/usr/lib/node_modules/cordova/node_modules/update-notifier/index.js:34:17) at module.exports (/usr/lib/node_modules/cordova/node_modules/update-notifier/index.js:123:23) at checkForUpdates (/usr/lib/node_modules/cordova/src/cli.js:64:20) at cli (/usr/lib/node_modules/cordova/src/cli.js:114:5) at Object. (/usr/lib/node_modules/cordova/bin/cordova:41:1) I am using Ubuntu 14.04 and I'm learning how to use linux and apache Cordova. I really appreciate any sort of help to help solve this problem.

tony berumen

the gift the keep on giving. thank you!


This was gold for me. Thank you very much!


You are amazing!! So helpful!!