NPM install permission denied error using root user - node.js

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!

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

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

#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

This works for me
sudo chown -R $(whoami) ~/.npm

Related

Error while running (npm install -g #angular/cli) in mac

I'm trying to install angular/cli and when I run
npm install -g #angular/cli
in the terminal I come up with these error messages, furthermore, I tried to use sudo as a prefix but again not a positive result.
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ssh://git#github.com/angular/cli.git
npm ERR!
npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.
npm ERR!
npm ERR! exited with error code: 128
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/username/.npm/_logs/2018-10-06T04_44_40_632Z- debug.log
npm packages should be installed without sudo in macos
Infact sudo should be never be used unless tinkering with system wide
permissions. Node puts npm packages in a specific folder, usually
/usr/local/lib/node_modules. But the trouble is you need sudo
permissions to write here. This leads to an endless use of
non-requisite sudo permissions. This location is what we need to
change and here are handy terminal commands to achieve the same:
mkdir ~/.npm
npm config set prefix ~/.npm
nano ~/.bashrc
export PATH="$PATH:$HOME/.npm/bin"
source ~/.bashrc
Quoted from here: https://medium.com/#Mandysidana/using-npm-install-without-sudo-2de6f8a9e1a3
Finally, I found the answer
Here is the code just run in terminal:
sudo npm install -g #angular/cli

gyp ERR! stack Error: EACCES: permission denied, mkdir '/var/www/project_name/node_modules/node-sass/build'

I'm deploying an Angular project on Ububtu 16.04 and get these errors when I run sudo npm install. (won't work without sudo). It seems that npm doesn't have permission to install. I recently updated to nodejs to 8.11.4 and still have the same error so it wasn't a version problem apparently. How can I give it the correct permissions?
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/var/www/project_name/node_modules/node-sass/build'
gyp ERR! System Linux 4.4.0-1065-aws
gyp ERR! command "/usr/local/bin/node" "/var/www/project_name/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "-libsass_library="
gyp ERR! cwd /var/www/front-stormsensor/node_modules/node-sass
gyp ERR! node -v v8.11.4
gyp ERR! node-gyp -v v3.8.0
gyp ERR! not ok
Build failed with error code: 1
Is there something off within my project? Or an incorrect installation? Not sure where to go from here, thank you
try the following command
sudo npm i --unsafe-perm
if it doesn't work try the following
sudo rm -rf ~/.node-gyp
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo npm i --unsafe-perm
I fixed it by run this command.
sudo npm i --unsafe-perm
In case you're installing something globally, you can use:
$ sudo npm install -g <package> --unsafe-perm
use following command to install
sudo npm install --unsafe-perm=true --allow-root
This solution from jnambiar worked for me.
My OS is OSX 10.11.6.
It's basically permissions issue.
sudo rm -rf ~/.node-gyp
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo npm i --unsafe-perm
Above set of commands helped me resolve the issue.
I am posting this to approve the set of commands which worked for me while I struggled to install the node-sass package for 2 days.
For anyone that might still have this problem, I solved it by following the suggestion of the second comment above and logging in as the root user on my ubuntu instance, as described in the second answer on this question here.. That allowed me to npm install without errors.
I used yarn instead of npm and it worked
npm permissions are quite a headache. The above solutions didn't work on my machine. Yarn did the trick.
sudo npm i yarn -g
yarn add
This command worked for me on windows.. sudo npm install --unsafe-perm

Error when using npm

I get the following error when trying to use npm on mac. I'm on mac 10.10.1
$ npm
Error: ENAMETOOLONG, mkdir '/Users/LM/npm info it worked if it ends with ok
npm verb cli [ '/usr/local/bin/node',
npm verb cli '/usr/local/bin/npm',
npm verb cli '-g',
npm verb cli 'config',
npm verb cli 'get',
npm verb cli 'prefix' ]
npm info using npm#2.1.6
npm info using node#v0.10.33
npm verb node symlink /usr/local/bin/node
/Users/LM/Desktop/Titanium Studio/TitaniumStudio.app/Contents/MacOS/npm info it worked if it ends with ok
npm verb cli [ '/usr/local/bin/node',
npm verb cli '/usr/local/bin/npm',
npm verb cli '-g',
npm verb cli 'config',
npm verb cli 'get',
npm verb cli 'prefix' ]
npm info using npm#2.1.6
npm info using node#v0.10.33
npm verb node symlink /usr/local/bin/node
/Users/LM/Desktop/Titanium Studio/TitaniumStudio.app/Contents/MacOS/npm info it worked if it ends with ok
npm verb cli [ '/usr/local/bin/node',
npm verb cli '/usr/local/bin/npm',
npm verb cli '-g',
npm verb cli 'config',
npm verb cli 'get',
npm verb cli 'prefix' ]
npm info using npm#2.1.6
npm info using node#v0.10.33
npm verb node symlink /usr/local/bin/node
/usr/local
npm verb exit [ 0, true ]
npm info ok
npm verb exit [ 0, true ]
npm info ok
npm verb exit [ 0, true ]
npm info ok'
Any ideas what this means and how to fix?
I think the issue is due to the permissions. Try to change the permissions as followed :
Unlock permissions in your home directory.
sudo chown -R `whoami` ~/.npm
If 1 does not work, get write permissions to the node_modules directory.
sudo chown -R `whoami` /usr/local/lib/node_modules
If you still face error, update permission of /usr/local.
sudo chown -R `whoami` /usr/local
Hope it helps.
The error ENAMETOOLONG means that nodejs (or the OS) is trying to use a pathname component that exceeds your OS's maximum (in the case of OSX, 31).
Firstly where have you tried to install nodejs from? This could be the cause of your issue. I've never used Titanium Studio but your question reads like this is the intall of nodejs that you are using.
You may be best installing node directly from github. The following should get you started.
Install Node
mkdir ~/src
cd ~/src
git clone https://github.com/joyent/node.git
cd node
git checkout v0.10.33
mkdir ~/local
./configure --prefix=$HOME/local/node
make
make install
Dont forget to add $HOME/local/node/bin to your PATH enviromental variable.
You can test your node installation by
node -v
Install NPM
curl http://npmjs.org/install.sh | sh
You can test your npm installation by
npm -v
There are several solutions suggested here, where somebody has the exact same problem, also using titanium studio: https://developer.appcelerator.com/question/179230/unable-to-properly-update-titanium-studio
Manually updating CLI, Node.ACS, and Alloy
sudo npm install -g alloy#1.5.1
sudo npm install -g titanium#3.4.1
sudo npm install -g acs
Change permissions to the directory
sudo chown -R whoami ~/.npm
Although it seems that the last thing did not work. You might want to have a look here as well: NPM throws error without sudo
1. Discover where npm executables exist
First, find out where npm executables exist on your system:
$ which -a npm
This should output two or more system paths.
2. Find out your shell's $PATH
Then, determine the search paths for your command execution:
$ echo $PATH
This will give you a colon-separated list of directories your session is searching for executables like npm.
3. Give priority to a different npm
Appcelerator Titanium has corrupted one or more aspects of your system. You may want to temporarily uninstall Titanium.
Otherwise, to give preference to another Node.js installation, you can edit your $PATH or even more simply, create a shell alias. For example:
$ alias npm=/usr/local/bin/npm
In most cases, you can "save" this in your ~/.bash_profile file (by simply copying and pasting the text as if you were at the prompt) so that it's applied to future shell sessions.

npm install error ENOTDIR

I am very new to Node.js and trying to install Flatiron using npm but it gives me an error.
sudo npm install flatiron -g
And I get -
npm http GET https://registry.npmjs.org/flatiron
npm http 304 https://registry.npmjs.org/flatiron
npm ERR! Error: ENOTDIR, mkdir '/home/siddharthsaha/tmp/npm-28554/1353323290836-0.20847953506745398'
npm ERR! If you need help, you may report this log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <npm-#googlegroups.com>
npm ERR! System Linux 3.2.0-24-generic
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "flatiron" "-g"
npm ERR! cwd /home/siddharthsaha/denarit
npm ERR! node -v v0.8.14
npm ERR! npm -v 1.1.65
npm ERR! path /home/siddharthsaha/tmp/npm-28554/1353323290836-0.20847953506745398
npm ERR! code ENOTDIR
npm ERR! errno 27
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/siddharthsaha/denarit/npm-debug.log
npm ERR! not ok code 0
What is wrong here? I have no clue.
Just solved the issue. Its because there's a file called tmp in the home directory.
rm -rf ~/tmp
sudo npm cache clear
sudo npm install -g node
Also... if you are trying to install npm then the same error and solution applies - delete ~/tmp
Try
sudo mkdir -p /home/siddharthsaha/tmp
sudo npm cache clear
before starting the install script, since npm http 304 https://registry.npmjs.org/flatiron line states that this module is coming from cache. And also ENOTDIR states that there is no directory. Therefore, emptying the cache would solve your problem.
Below are the steps to install a given release from source without root
NOTE - this installs nodejs which gives you both node as well as npm,
they come together per release.
to start fresh remove prior node.js and npm installs as well as these :
sudo mv ~/.npmrc ~/.npmrc_ignore
sudo mv ~/.npm ~/.npm_ignore
sudo mv ~/tmp ~/tmp_ignore
sudo mv ~/.npm-init.js ~/.npm-init.js_ignore
to install nodejs and npm as yourself NOT root do these commands (OSX/linux) :
export NODE_PARENT=${HOME}/bin_0_10_32
mkdir ${NODE_PARENT}
download source from : http://nodejs.org/download/
cd node-v0.xxxx
./configure --prefix=${NODE_PARENT}/nodejs
make -j8
make install # IMPORTANT this is NOT using sudo
# not wanted since installing into $USER owned $NODE_PARENT
which puts it into dir defined by above --prefix
export PATH=${NODE_PARENT}/nodejs/bin:$PATH
define environment variable NODE_PATH so node can find dir for modules otherwise
npm install xxx will put newly installed module into current dir :
export NODE_PATH=${NODE_PARENT}/nodejs/lib/node_modules
when you use syntax : npm install -g some_cool_module
the -g for global installs it into dir $NODE_PATH and not your $PWD
nodejs install gives you npm as well :
ls -la ${NODE_PARENT}/nodejs/bin
Subsequent modules you install using global flag -g will automagically put
their ~binaries~ into above bin dir ... like browserify
Now put above three export xxx=yyy
commands into your ~/.bashrc or some such so your environment is setup
I direct delete the file of npm-debug.log.
then it's ok for me.

npm packager installation errors

I ran the command -
sudo curl http://npmjs.org/install.sh | sh
But I am getting this Error. Can anyone help?
Password:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
101 7881 101 7881 0 0 5165 0 0:00:01 0:00:01 --:--:-- 7696
tar=/usr/bin/tar
version:
bsdtar 2.6.2 - libarchive 2.6.2
install npm#1.0
fetching: http://registry.npmjs.org/npm/-/npm-1.0.106.tgz
0.5.11-pre
1.0.106
cleanup prefix=/usr/local
All clean!
npm ERR! Could not create /usr/local/lib/node_modules/___npm.npm
npm ERR! error installing npm#1.0.106 Error: EACCESS, Permission denied '/usr/local/lib/node_modules'
npm ERR! Error: EACCESS, Permission denied '/usr/local/lib/node_modules'
npm ERR! Report this *entire* log at:
npm ERR! <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR! <npm-#googlegroups.com>
npm ERR!
npm ERR! System Darwin 10.8.0
npm ERR! command "/usr/local/bin/node" "/private/var/folders/a4/a4oBVIXZEDS3kWEMo4Gh8U+++TI/-Tmp-/npm.4405/package/cli.js" "install" "-gf"
npm ERR! cwd /private/var/folders/a4/a4oBVIXZEDS3kWEMo4Gh8U+++TI/-Tmp-/npm.4405/package
npm ERR! node -v v0.5.11-pre
npm ERR! npm -v 1.0.106
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCESS
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /private/var/folders/a4/a4oBVIXZEDS3kWEMo4Gh8U+++TI/-Tmp-/npm.4405/package/npm-debug.log
npm not ok
It failed
They recommend to not use Sudo on this site: http://howtonode.org/introduction-to-npm, and to instead use:
sudo chown -R $USER /usr/local
Followed by:
curl http://npmjs.org/install.sh | sh
Call the command using sudo:
sudo curl http://npmjs.org/install.sh | sudo sh
Below are the steps to install a given release from source
NOTE - this installs nodejs which gives you both node as well as npm,
they come together per release.
to start fresh remove prior node.js and npm installs as well as these :
sudo mv ~/.npmrc ~/.npmrc_ignore
sudo mv ~/.npm ~/.npm_ignore
sudo mv ~/tmp ~/tmp_ignore
sudo mv ~/.npm-init.js ~/.npm-init.js_ignore
to install nodejs and npm as yourself NOT root do these commands (OSX/linux) :
export NODE_PARENT=${HOME}/bin_0_10_32
mkdir ${NODE_PARENT}
download source from : http://nodejs.org/download/
cd node-v0.xxxx
./configure --prefix=${NODE_PARENT}/nodejs
make -j8
make install # IMPORTANT this is NOT using sudo
# not wanted since installing into $USER owned $NODE_PARENT
which puts it into dir defined by above --prefix
export PATH=${NODE_PARENT}/nodejs/bin:$PATH
define environment variable NODE_PATH so node can find dir for modules otherwise
npm install xxx will put newly installed module into current dir :
export NODE_PATH=${NODE_PARENT}/nodejs/lib/node_modules
when you use syntax : npm install -g some_cool_module
the -g for global installs it into dir $NODE_PATH and not your $PWD
nodejs install gives you npm as well :
ls -la ${NODE_PARENT}/nodejs/bin
Subsequent modules you install using global flag -g will automagically put
their ~binaries~ into above bin dir ... like browserify
Now put above three export xxx=yyy
commands into your ~/.bashrc or some such so your environment is setup

Resources