NPM Throws Error without Sudo

If you know about JavaScript then you know about NPM; it’s the default package manager for Node.js which is an open source runtime environment. Developers use NPM because not only does it provide an easy way to maintain the code but it also helps them when they have to share it with other developers who can easily reuse the code during their own development.

An NPM file or an NPM package is nothing but logically separated reusable code that's organized as a directory with some files in it. One of the files you'll always find is the "package.json" file, which contains metadata about the package among other things. NPM installation can often lead to strange issues if not done properly. In this article I will be talking about the "NPM throwing errors without sudo" issue that developers sometimes face. If you encounter the error, it might look something like this:

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

npm ERR! System Darwin 15.5.0  
npm ERR! command "node" "/usr/local/bin/npm" "search" "babel"  
npm ERR! cwd /Users/scott  
npm ERR! node -v v0.10.4  
npm ERR! npm -v 1.2.18  
npm ERR! path /Users/scott/.npm/-/all/.cache.json  
npm ERR! code EACCES  
npm ERR! errno 3  
npm ERR! stack Error: EACCES, open '/Users/scott/.npm/-/all/.cache.json'  
npm ERR!  
npm ERR! Additional logging details can be found in:  
npm ERR!     /Users/scott/npm-debug.log  
npm ERR! not ok code 0  

So basically, whenever you try to install or search something via NPM, you get a long list of unhelpful errors and the command you're trying to run fails. When you try it with "sudo" however, you get the desired output. This will happen even if you are logged in as an administrator. This problem normally occurs when there is some issue with the permissions and privileges pertaining to your NPM installation.

Did you install NPM using root privileges? That can be one of the reasons to cause you to need root privileges every time you run a command. Or this could also be caused by permission issues within your home directory in which case you will have to take back the ownership, which is one of the solutions I've listed below.

Solution 1: Install via NVM

"Everything is safe with root permissions" is not a cliché and for good reason. When you install and run node on a development machine with root privileges, it won’t bite you until you try to install other packages with it and face the under-discussion issue.

The Node Version Manager, or NVM, is a tool that allows developers to install as many versions of Node you need and without requiring root privileges too. I recommend using it and performing a reinstall after uninstalling the current NPM installation. If this is the route you want to take, then follow these steps:

  1. Fire up a terminal and enter the following command to remove all the global Node modules:

    $ npm ls -gp --depth=0 | awk -F/ '/node_modules/ && !/\/npm$/ {print $NF}' | xargs npm -g rm
  2. Go to the official NVM git repo and follow the instructions there to install NVM. To ensure that NVM has been installed successfully, enter the command below and see if the output's a version number:

    $ nvm --version

    If "nvm" was output to the console, as shown above, then it is has been installed.

  3. Once you have ensured that NVM has gotten installed on your device, run this command to install node:

    $ nvm install stable

Once you are done, try running npm link or perform an NPM global install and you will see that you no longer require root privileges for it to work.

Solution 2: Taking Ownership

As I mentioned earlier, much of the time this problem is caused by issues in your home directory permissions. If you are doubtful that this is not the case for you then don’t worry, you can try it anyway because even if it isn’t the cause, it won’t do you any harm.

Once you are set, fire up the terminal and enter the following command:

$ sudo chown -R $(whoami) ~/.npm

The $(whoami) part will take your user name and place it there within the command, making you the owner of all files/folders in ~/.npm.

When asked to provide your password, please go ahead. Once you are done, try to install or search via NPM again and see if the problem persists; it shouldn’t.

Solution 3: Uninstall all NPM modules and reinstall globally with root privileges

If you are a Linux or an OSX user, you can also create a dedicated directory to hold your global package. You can then setup NPM and node to find the globally installed packages on their own. To do a global install for a specific user (without root privileges) follow these steps:

  1. As a first step, make a directory for the global packages:

    $ mkdir "${HOME}/.npm-packages"
  2. Go to the "~/.npmrc" file and add this at the end:

  3. Go to your ".bashrc" or ".zshrc" files and add:

    # Unset manpath so we can inherit from /etc/manpath via the `manpath` command
    unset MANPATH # delete if you already modified MANPATH elsewhere in your config
    export MANPATH="$NPM_PACKAGES/share/man:$(manpath)"

This will ensure that NPM finds the man pages and the installed binaries easily.

Note: For Mac users, the ".bashrc" file often doesn’t exist and the terminal obtains its parameters from other files like the ".profile" or the "bash_profile" files. In such a scenario, add the following line to the end of any of the aforementioned files to ensure that the terminal also includes the ".bashrc" file:

$ source ~/.bashrc

If you don’t want to manually perform the above steps, then you can check out the nice shell script npm-g_nosudo, which performs all these steps automatically for you.


I am sure that after trying one of the solutions above, you should no longer have a problem with this error. If you have a choice, then I'd highly recommend using NVM to install different versions of Node/NPM. Not only will it solve your problem with using NPM with sudo, but it also lets you easily install and switch between multiple versions of Node/NPM while you're developing. It's been a lifesaver for me, to say the least.

Let us know in the comments how things worked out for you!