Using sudo is not recommended. It may give you permission issue later. While the above works, I am not a fan of changing folders owned by root to be writable for users, although it may only be an issue with multiple users. To work around that, you could use a group, with 'npm users' but that is also more administrative overhead. See here for the options to deal with permissions from the documentation: https://docs.npmjs.com/getting-started/fixing-npm-permissions
I would go for option 2:
To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, it will be a hidden directory on your 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
Test: Download a package globally without using sudo.
npm install -g jshint
If still show permission error run (mac os):
sudo chown -R $USER ~/.npm-global
This works with the default ubuntu install of:
sudo apt-get install nodejs npm
I recommend nvm
if you want more flexibility in managing versions:
https://github.com/creationix/nvm
On MacOS use brew, it should work without sudo
out of the box if you're on a recent npm
version.
Enjoy :)
Run these commands in a terminal window (note: DON'T replace the $USER part... thats a linux command to get your user!):
sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/lib/node_modules
sudo chown -R $USER /usr/local/lib/node_modules
Mac OS X Answer
You don't have write access to the node_modules directory
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
Add your User to the directory with write access
Open folder containing node_modules
open /usr/local/lib/
Use sudo -i to switch to $root, then execute npm install -g xxxx
Reinstall node and npm with Node Version Manger (as per written in npm documentation) to avoid permission errors:
In OSX:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
or follow this article:
http://dev.topheman.com/install-nvm-with-homebrew-to-use-multiple-versions-of-node-and-iojs-easily/
Windows users should install nvm-windows. For further help how to install nvm refer the nvm readme.
Then choose for example:
nvm install 8.0.0
nvm use 8.0
Now you can give another try:
npm install -g less
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With