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
In my case,it's because of the permission of ~/tmp.So I do:
sudo chown -R $USER ~/tmp
And it's OK!
Permissions you used when installing Node will be required when doing things like writing in your npm directory (npm link
, npm install -g
, etc.).
You probably ran node installation with root permissions, that's why the global package installation is asking you to be root.
Don't hack with permissions, install node the right way.
On a development machine, you should not install and run node with root permissions, otherwise things like npm link
, npm install -g
will need the same permissions.
NVM (Node Version Manager) allows you to install Node without root permissions and also allows you to install many versions of Node to play easily with them.. Perfect for development.
nvm install node
Now npm link
, npm install -g
will no longer require you to be root.
Edit: See also https://docs.npmjs.com/getting-started/fixing-npm-permissions
webi fetches the official node
package from the node release API. It does not require a package manager, does not require sudo
or root
access, and will not change any system permissions.
curl -s https://webinstall.dev/node | bash
Or, on Windows 10:
curl.exe -sA "MS" https://webinstall.dev/node | powershell
Like nvm
, you can easily switch node versions:
webi node@v12
Unlike nvm
(or Solution 3 below), the npm
packages will be separate (you will need to re-install when you switch node versions).
Without changing npm
configuration, you can install globally:
npm install -g prettier
This solution is essentially an automated version of other solutions that install to $HOME
.
Don't hack with permissions, install npm packages globally the right way.
If you are on OSX or Linux, you can create a user dedicated directory for your global package and setup npm
and node
to know how to find globally installed packages.
Check out this great article for step by step instructions on installing npm modules globally without sudo.
See also: npm's documentation on Fixing npm permissions.
For me, execute only
sudo chown -R $(whoami) ~/.npm
doesn't work. Then, I execute too
sudo chown -R $(whoami) /usr/lib/node_modules/
sudo chown -R $(whoami) /usr/bin/node
sudo chown -R $(whoami) /usr/bin/npm
And all works fine!
Other answers are suggesting to change ownerships or permissions of system directories to a specific user. I highly disadvise from doing so, this can become very awkward and might mess up the entire system!
Here is a more generic and safer approach that supports multi-user as well.
Create a new group for node-users and add the required users to this group. Then set the ownership of node-dependant files/directories to this group.
# Create new group
sudo groupadd nodegrp
# Add user to group (logname is a variable and gets replaced by the currently logged in user)
sudo usermod -a -G nodegrp `logname`
# Instant access to group without re-login
newgrp nodegrp
# Check group - nodegrp should be listed as well now
groups
# Change group of node_modules, node, npm to new group
sudo chgrp -R nodegrp /usr/lib/node_modules/
sudo chgrp nodegrp /usr/bin/node
sudo chgrp nodegrp /usr/bin/npm
# (You may want to change a couple of more files (like grunt etc) in your /usr/bin/ directory.)
Now you can easily install your modules as user
npm install -g generator-angular
Some modules (grunt, bower, yo etc.) will still need to be installed as root. This is because they create symlinks in /user/bin/.
Edit
3 years later I'd recommend to use Node Version Manager. It safes you a lot of time and trouble.
I had a similar problem at NPM modules won't install globally without sudo, the issue was that when i installed node i did it with sudo via chris/lea ppa repo.
My solution was to uninstall node and then install it this way:
Download latest stable node sources from nodejs.org #in my case node-v0.10.20.tar.gz
tar -zxf node-v0.10.20.tar.gz #uncompress sources
cd node-v0.10.20 #enter uncompressed folder
sudo chown $USER -R /usr/local
./configure --prefix=/usr/local && make && make install
PD: If you don't want to change ownership of the /usr/local folder, you can install it somewhere you already own. The problem of this approach is that you will have to bind the installation folder with the bash command line so that we can use the node command later on
mkdir ~/opt
./configure --prefix=~/opt && make && make install
echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc #or ~/.profile or ~/.bash_profile or ~/.zshenv depending on the current Operative System
With either of those approaches, you will be able to do the following without using sudo
npm install -g module_to_install
On Mac OS X, when installing with Homebrew's brew install npm
, the installation path is /usr/local/share/npm/
with both bin/
and lib/node_modules/
subfolders.
Running this command to change to owner to your currently logged in user should fix it all up, and allow you to install global NPM packages without sudo
.
sudo chown -R $USER ~/.npm /usr/local/share/npm/
osx homebrew