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
Changing the owner on "system-global" folders is a hack. On a fresh install, I would configure NPM to use an already writable location for "user-global" programs:
npm config set prefix ~/npm
Then make sure you add that folder to your path:
export PATH="$PATH:$HOME/npm/bin"
See @ErikAndreas' answer to NPM modules won't install globally without sudo
and longer step-by-step guide by @sindresorhus with also sets $MANPATH
.
When you run npm install -g somepackage, you may get an EACCES error asking you to run the command again as root/Administrator. It's a permissions issue.
It's easy to fix, open your terminal (Applications > Utilities > Terminal)
sudo chown -R $USER /usr/local/lib/node_modules
** I strongly recommend you to not use the package management with sudo (sudo npm -g install something), because you can get some issues later **
Reference: http://foohack.com/2010/08/intro-to-npm/
In case sudo chown -R $(whoami) ~/.npm
didn't work for you, or you need a non terminal command solution.
The issue is that your user account does not have write permission to node_modules folder, so you can do the following
Open finder and press cmd
+ shift
+ g
this will open go to folder with url
Write the following path /usr/local/lib/node_modules
and press go
Right click on node_modules
folder and choose Get Info
Scroll down to sharing & permissions
section
Unlock to be able to make changes.
Press +
and add your user account
Make sure that you choose Read & Write
in privilege drop down
Now you should be able to install packages without sudo
and permission issues should be solved
@Yves M.'s answer was very similar to my solution. Here are the commands I used, which were slightly different from his.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash
Then query for the latest version:
nvm ls-remote
Then install the newest version:
nvm install YOUR_VERSION_HERE
example
nvm install v5.8.0
TL;DR
always use
sudo -i
orsudo -H
when runningnpm install
to install global packages.
When you use npm
, it downloads packages to your user home directory. When you run as sudo, npm
installs files to the same directory, but now they are owned by root.
So this is what happens to absolutely every single person who has ever used npm
:
npm install foo
sudo install -g foo-cli
without issuenpm install bar
npm
designers now that you have to go chmod
a directory againWhen you use the -i
or -H
option with sudo, your home directory will be root
's home directory. Any global installs will cache packages to /root/.npm
instead of root
-owned files at /home/me/.npm
.
Just always use sudo -i
or sudo -H
when running npm install
to install global packages and your npm
permissions problems will melt away.
For good.
http://hood.ie/blog/why-you-shouldnt-use-sudo-with-npm.html
--
q.v. the accepted answer for fixing an already messed up npm
.
Best solution would be this which is provided by npm documentation.
For Ubuntu suggested solution is Option#2
Brief steps:
Make a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
npm config get prefix
can help you to verify if prefix was updated or not. The result would be <Your Home Directory>/.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
Instead of steps 2-4 you can also use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):
NPM_CONFIG_PREFIX=~/.npm-global
For Mac suggested solution is Option#3
On Mac OS you can avoid this problem altogether by using the Homebrew package manager
brew install node