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
ISSUE: You (the user) don't have the right set of permissions for the directory.
The instant way out is to run the npm install using sudo, but this may give you the same error, or improper installation.
AND changing directory ownership is not a good option, a temporary patch.
Solution/Suggestion: Change npm's Default Directory (from official docs)
Back-up your computer before moving forward.
(optional) In case you have a erroneous installation, first uninstall it:
npm uninstall <package-name> # use sudo if you used it while installation
npm cache verify # or, npm cache clean for npm version below 5.x.x
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
or ~/.bash_profile
file and add this line:
export PATH=~/.npm-global/bin:$PATH
Back on the command line, update your system variables, or restart the terminal:
source ~/.profile
(optional) Test: Download a package globally without using sudo.
npm install -g jshint
I Solve it by changing the owner from root to my user-name
sudo chown -R me:me /home/me/.config/configstore/
change me with your user-name and group .
I like to use ubuntu groups to achieve this instead of changing owner. Its quite simple.
First install nodejs and npm using apt-get
sudo apt-get update && sudo apt-get install nodejs npm
Figure out who is logged in i.e username, run following command to see it in terminal
whoami
You can see the list of groups you are assigned by using a very simple command, normally the first group is your username itself
groups
Run following to allow access to logged in user
sudo chmod 777 -R /usr/local && sudo chgrp $(whoami) -R /usr/local
Update npm and nodejs
npm install -g npm
You are allset, your user can run npm commands without sudo
You can also refer to this link https://askubuntu.com/a/1115373/687804
As if we need more answers here, but anyway..
Sindre Sorus has a guide Install npm packages globally without sudo on OS X and Linux outlining how to cleanly install without messing with permissions:
Here is a way to install packages globally for a given user.
Create a directory for your global packages
mkdir "${HOME}/.npm-packages"
Reference this directory for future usage in your .bashrc/.zshrc:
NPM_PACKAGES="${HOME}/.npm-packages"
Indicate to npm where to store your globally installed package. In your
$HOME/.npmrc
file add:prefix=${HOME}/.npm-packages
Ensure node will find them. Add the following to your .bashrc/.zshrc:
NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
Ensure you'll find installed binaries and man pages. Add the following to your
.bashrc
/.zshrc
:PATH="$NPM_PACKAGES/bin:$PATH" # 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 MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
Check out npm-g_nosudo for doing the above steps automagically
Checkout the source of this guide for the latest updates.
For Mac (adopted from Christoper Will's answer)
Mac OS X 10.9.4
System Preference > Users & Groups > (unlock) > press + :
New Account > "Group"
Account Name : nodegrp
After creating the group, tick the user to be included in this group
sudo chgrp -R nodegrp /usr/local/lib/node_modules/
sudo chgrp nodegrp /usr/bin/node
sudo chgrp nodegrp /usr/bin/npm
sudo chown -R $(whoami):nodegrp ~/.npm
Problem: You do not have permission to write to the directories that npm uses to store global packages and commands.
Solution: Allow permission for npm.
Open a terminal:
command + spacebar then type 'terminal'
Enter this command:
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
This solution allows permission to ONLY the directories needed, keeping the other directories nice and safe.