Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm uninstalls itself on npm update

Tags:

node.js

npm

I looked for a similar topic in stackoverflow but could not find one, so here it goes.

I've been looking at MEAN stack documentation at http://learn.mean.io/ and just checking some shell commands.

I tried to update npm via

npm update -g npm

but this came out to give errors

npm ERR! Error: EACCES, unlink '/usr/local/lib/node_modules/npm'
npm ERR!  { [Error: EACCES, unlink '/usr/local/lib/node_modules/npm']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/usr/local/lib/node_modules/npm' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 14.0.0
npm ERR! command "node" "/usr/local/bin/npm" "update" "-g" "npm"
npm ERR! cwd /Users/cihankoseoglu
npm ERR! node -v v0.10.33
npm ERR! npm -v 1.4.28
npm ERR! path /usr/local/lib/node_modules/npm
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, unlink '/usr/local/lib/node_modules/npm'
npm ERR! error rolling back Error: EACCES, unlink '/usr/local/lib/node_modules/npm'
npm ERR! error rolling back  { [Error: EACCES, unlink '/usr/local/lib/node_modules/npm']
npm ERR! error rolling back   errno: 3,
npm ERR! error rolling back   code: 'EACCES',
npm ERR! error rolling back   path: '/usr/local/lib/node_modules/npm' }
npm ERR! not ok code 0

so i tried

sudo npm update -g npm

but after this command runs , my zsh shell can't find npm. when i type any npm command it simply returns command not found. I had to reinstall Node. what could be possibly causing this? Is it uninstalling npm or altering its PATH in the background for reasons unbeknowst to me?

like image 207
Cihan Köseoğlu Avatar asked Nov 01 '22 13:11

Cihan Köseoğlu


1 Answers

This is a tried and proven Node.js install technique, afterwards your npm update command will run OK

Below are the steps to install Node.js from source (OSX/linux) You may/should issue all these cmds as yourself NOT root (sudo)

NOTE - this installs Node.js which gives you both node as well as npm, they come together per release.

to start fresh remove prior node 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

download source from : https://nodejs.org/en/download/

cd node-v13.6.0

define environment variable NODE_PATH as the dir for subsequent module installs

export NODE_PARENT=/some/desired/install/path_goes_here

export NODE_PARENT=/usr/local/bin/nodejs # use this if you want to install as root (sudo)
export NODE_PARENT=${HOME}/node-v13.6.0  # use this if you want to install modules as yourself

export PATH=${NODE_PARENT}/bin:${PATH}
export NODE_PATH=${NODE_PARENT}/lib/node_modules

./configure   --prefix=${NODE_PARENT}

make -j8      #  if you have a quad core use -j8 
make install  #  NOTICE not using sudo so install is owned by normal user

which puts it into dir defined by above --prefix

verify its installed OK just issue

node --version

v13.6.0

when you use syntax :

npm install -g some_cool_module

the -g for global installs it into dir $NODE_PATH and not your $PWD

Now put above three export xxx=yyy commands into your ~/.bashrc or some such to persist these environment variable changes

going forward when you want to install some npm package avoid using sudo

like image 148
Scott Stensland Avatar answered Nov 15 '22 04:11

Scott Stensland