NPM install permission denied error using root user
Creating a new global storage in your user space is the recommended way of preventing this error. Don't use sudo or change ownership of locations.
$ mkdir ~/.npm-global
$ npm config set prefix '~/.npm-global'
$ export PATH=~/.npm-global/bin:$PATH
$ source ~/.profile
However, since you're using npm 5.6, this might be more relevant to you:
npx: an alternative to running global commands
If you are using npm version 5.2 or greater, you may want to consider npx as an alternative way to run global commands, especially if you only need a command occasionally. For more information, see this article about npx.
Source: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
Chown down your node_modules & do not run as sudo.
You should always have ownership of the .npm
directory as NPM will refuse installing native modules with sudo as this can be a security risk.
Try sudo chown -R $(whoami) ~/.npm
and see if this would work. (or sudo chown -R $(whoami) ~/.nvm
if you're using nvm)
Or reinstall with a node version manager without root as recommended in the docs:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
@U-ways answer is very good but for people who use nvm rather than:
sudo chown -R $(whoami) ~/.npm
do
sudo chown -R $(whoami) ~/.nvm
this works form me