PenAndPapers
PenAndPapers

Reputation: 2108

NPM install permission denied error using root user

So I've made a fresh installation of npm/node on my local machine using NVM using root user and everything looks fine, now my issue is when I tried to install using npm install --unsafe-perm -verbose command on my project folder error displays in my terminal.

npm verb stack Error: Command failed: /usr/bin/git clone --depth=1 -q -b 0.0.7 https://github.com/Mango/emitter.git /root/.npm/_cacache/tmp/git-clone-28a98ad9
npm verb stack fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-28a98ad9': Permission denied
npm verb stack 
npm verb stack     at ChildProcess.exithandler (child_process.js:282:12)
npm verb stack     at ChildProcess.emit (events.js:182:13)
npm verb stack     at maybeClose (internal/child_process.js:957:16)
npm verb stack     at Socket.stream.socket.on (internal/child_process.js:378:11)
npm verb stack     at Socket.emit (events.js:182:13)
npm verb stack     at Pipe._handle.close [as _onclose] (net.js:598:12)
npm verb cwd /web/nbltv
npm verb Linux 4.15.0-29-generic
npm verb argv "/root/.nvm/versions/node/v10.1.0/bin/node" "/root/.nvm/versions/node/v10.1.0/bin/npm" "install" "--unsafe-per" "-verbose"
npm verb node v10.1.0
npm verb npm  v5.6.0
npm ERR! code 128
npm ERR! Command failed: /usr/bin/git clone --depth=1 -q -b 0.0.7 https://github.com/Mango/emitter.git /root/.npm/_cacache/tmp/git-clone-28a98ad9
npm ERR! fatal: could not create leading directories of '/root/.npm/_cacache/tmp/git-clone-28a98ad9': Permission denied
npm ERR! 
npm verb exit [ 1, true ]

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2018-08-20T01_36_33_496Z-debug.log

NPM version - 5.6 NODE version - 10.1

Any help would be much appriciated. Thanks!

Upvotes: 10

Views: 30935

Answers (4)

u-ways
u-ways

Reputation: 7714

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

Upvotes: 22

MD SHAYON
MD SHAYON

Reputation: 8055

This works for me

sudo chown -R $(whoami) ~/.npm

Upvotes: 1

Lilian Bideau
Lilian Bideau

Reputation: 307

@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

Upvotes: 1

aalaap
aalaap

Reputation: 4401

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

Upvotes: 9

Related Questions