I just installed node and npm through the package on nodejs.org and whenever I try to search or install something with npm it throws the following error, unless I sudo the c
you could try this, works on ubuntu and mac
sudo chown -R $(whoami) /usr/local/lib/node_modules
John Papa points to the history and reasoning behind this issue and gives a solid fix:
John Papa's steps are to:
Hope this helps the curious!
What to me seems like the best option is the one suggested in the npm documentation, which is to first check where global node_modules are installed by default by running npm config get prefix
. If you get, like I do on Trusty, /usr
, you might want to change it to a folder that you can safely own without messing things up the way I did.
To do that, choose or create a new folder in your system. You may want to have it in your home directory or, like me, under /usr/local
for consistency because I'm also a Mac user (I prefer not to need to look into different places depending on the machine I happen to be in front of). Another good reason to do that is the fact that the /usr/local
folder is probably already in your PATH (unless you like to mess around with your PATH) but chances are your newly-created folder isn't and you'd need to add it to the PATH yourself on your .bash-profile or .bashrc file.
Long story short, I changed the default location of the global modules with npm config set prefix '/usr/local'
, created the folder /usr/local/lib/node_modules
(it will be used by npm) and changed permissions for the folders used by npm with the command:
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
Now you can globally install any module safely. Hope this helps!
This looks like a permissions issue in your home directory. To reclaim ownership of the .npm directory execute:
sudo chown -R $(whoami) ~/.npm
The official documentation on how to fix npm install
permissions with an EACCES
error is located at https://docs.npmjs.com/getting-started/fixing-npm-permissions.
I encountered this problem after a fresh install of node using the .pkg
installer on OSX. There are some great answers here, but I didn't see a link to npmjs.com yet.
Option 1: Change the permission to npm's default directory
Find the path to npm's directory:
npm config get prefix
For many systems, this will be /usr/local.
WARNING: If the displayed path is just /usr, switch to Option 2.
Change the owner of npm's directories to the name of the current user (your username!):
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).
Option 2: Change npm's default directory to another directory
There are times when you do not want to change ownership of the default directory that npm uses (i.e. /usr) as this could cause some problems, for example if you are sharing the system with other users.
Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.
Make a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
Back on the command line, update your system variables:
source ~/.profile
Watch OUT!!! Watch OUT!!! Watch OUT!!!
chown or chmod is NOT the solution, because of security-risk.
Instead do this, do:
First check, where npm point to, if you call:
npm config get prefix
If /usr is returned, you can do the following:
mkdir ~/.npm-global
export NPM_CONFIG_PREFIX=~/.npm-global
export PATH=$PATH:~/.npm-global/bin
This create a npm-Directory in your Home-Directory and point npm
to it.
To got this changes permanent, you have to add the export-command to your .bashrc:
echo -e "export NPM_CONFIG_PREFIX=~/.npm-global\nexport PATH=\$PATH:~/.npm-global/bin" >> ~/.bashrc