Cli: [BUG] `npm install -g npm` removes `npm` command

Created on 18 Dec 2019  ·  19Comments  ·  Source: npm/cli

What / Why

npm command is removed after running npm install -g npm.

When



  • 2019/12/18

  • I can't remember exactly when I succeeded in running this command but at least a week ago.

Where



  • npm/cli

How

Current Behavior


This is the log:

~
% brew install node
==> Downloading https://homebrew.bintray.com/bottles/node-13.3.0_1.catalina.bottle.tar.gz
Already downloaded: /Users/manabu/Library/Caches/Homebrew/downloads/8a482358c0487b3ffeeebf4d1f3b3ca825018f6318ecdd6368fd0a6f45e0bea1--node-13.3.0_1.catalina.bottle.tar.gz
==> Pouring node-13.3.0_1.catalina.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d
==> Summary
🍺  /usr/local/Cellar/node/13.3.0_1: 4,663 files, 59MB

~
% npm install -g npm
/usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
npm ERR! code EEXIST
npm ERR! syscall symlink
npm ERR! path ../../../lib/node_modules/npm/man/man1/npm-access.1
npm ERR! dest /usr/local/share/man/man1/npm-access.1
npm ERR! errno -17
npm ERR! EEXIST: file already exists, symlink '../../../lib/node_modules/npm/man/man1/npm-access.1' -> '/usr/local/share/man/man1/npm-access.1'
npm ERR! File exists: /usr/local/share/man/man1/npm-access.1
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/manabu/.npm/_logs/2019-12-18T07_51_00_280Z-debug.log

I confirmed that this happened again when I ran brew reinstall node.

Actually as the output said, I successfully ran it with --force but I think that just showing these logs is better than removing the npm command.

Edit (2020/01/11):

Actually as the output said, I successfully ran it with --force

More specifically, I did these steps:

  1. Restore npm command that was removed by npm install -g npm

    • Since I use Homebrew for managing Node.js, I ran brew reinstall node to restore it

  2. Run npm install -g --force npm as the output mentioned

Steps to Reproduce

  • n/a

Expected Behavior

  • Updating npm is successful

Who



  • n/a

References



  • n/a

Bug

Most helpful comment

Summarized the solution is:

brew reinstall node
npm i -g --force npm

All 19 comments

related to @isaacs' change in https://github.com/npm/cli/commit/320ac9aeeafd11bb693c53b31148b8d10c4165e8 npm/bin-links#12 npm/gentle-fs#7 released in [email protected]

surfaced by the bump of npm from 6.13.2 to 6.13.4 in homebrew https://github.com/Homebrew/homebrew-core/commit/6acc21a27d0ba4ecfd6ebf4d77af7025143daefe#diff-d3ef2414308e30df24a517ca9a1324ee (authored/commited by @pepyakin @fxcoudert @bayandin )

AFAICS this is intended behaviour in npm. Unclear for me how this should be handled in a homebrew setup.

https://github.com/npm/cli/issues/672

I did yarn global add npm and it installed [email protected] and I did not encounter the error, but now all my previous global packages are gone :((

I opted out from homebrew and simply used https://github.com/nvm-sh/nvm

Just do

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

Then somewhere on the end, you'll see

=> Compressing and cleaning up git repository

=> Appending nvm source string to /Users/aprilmintacpineda/.bash_profile
=> Appending bash_completion source string to /Users/aprilmintacpineda/.bash_profile
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Just do source <name of profile> In my case <name of profile> is ~/.bash_profile so it will be source ~/.bash_profile

then

nvm install node to install the latest version of node, it will also install the latest version of npm, and that worked for me.

not sure if it's clear to everyone on this thread that the solution, given that you know what you're doing and why, is simply

npm i -g --force npm (note the --force flag)

The problem is, after doing the npm -I npm and getting the error, trying any npm command fails with 'zsh: command not found: npm', so I could not do the --force command even if I wanted to. I had to brew reinstall node to get npm back.

Sorry guys, I didn't fully describe how I temporarily avoided this issue. I've updated the description, please have a look.

not sure if it's clear to everyone on this thread that the solution, given that you know what you're doing and why, is simply

npm i -g --force npm (note the --force flag)

This was the only solution I found to fix this actually.

2nd time this happens to me in a few weeks. You can fix it with brew reinstall node.

Here's the output I get running npm install -g npm:

/usr/local/bin/npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js
/usr/local/bin/npx -> /usr/local/lib/node_modules/npm/bin/npx-cli.js
npm ERR! code EEXIST
npm ERR! syscall symlink
npm ERR! path ../../../lib/node_modules/npm/man/man1/npm-adduser.1
npm ERR! dest /usr/local/share/man/man1/npm-adduser.1
npm ERR! errno -17
npm ERR! EEXIST: file already exists, symlink '../../../lib/node_modules/npm/man/man1/npm-adduser.1' -> '/usr/local/share/man/man1/npm-adduser.1'
npm ERR! File exists: /usr/local/share/man/man1/npm-adduser.1
npm ERR! Remove the existing file and try again, or run npm
npm ERR! with --force to overwrite files recklessly.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/albertvilacalvo/.npm/_logs/2020-01-13T11_55_47_922Z-debug.log

Summarized the solution is:

brew reinstall node
npm i -g --force npm

This happens not only with the homebrew install. I have the same issue with the nodejs installer (from the official site). The fix works, though... (sudo npm i -g --force npm)

Updating npm deletes npm? And it is not fixed since december?

Updating npm deletes npm? And it is not fixed since december?

Yep, it just happened to me also on MacOS 10.15.2

Just happened to me as well. MacOS Catalina 10.15.3

This just happened to me on Ubuntu, and I do not use "brew".

I ended up trying to reinstall npm, but it wouldn't let me install it without sudo. Installed with sudo, then found an (apparently bad) guide to reclaim ownership of the ~/.npm directory (and a couple others). At first it still didn't work, but hash -r fixed it (in retrospect, restarting my Ubuntu might have worked too). There's a slim possibility this also fixed an issue I was having with react-snap.

This bug was reported in 2014 and it happened to me today in 2020. What a disaster. Is this ever going to get fixed?

I opted out from homebrew and simply used https://github.com/nvm-sh/nvm

Just do

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

Then somewhere on the end, you'll see

=> Compressing and cleaning up git repository

=> Appending nvm source string to /Users/aprilmintacpineda/.bash_profile
=> Appending bash_completion source string to /Users/aprilmintacpineda/.bash_profile
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

Just do source <name of profile> In my case <name of profile> is ~/.bash_profile so it will be source ~/.bash_profile

then

nvm install node to install the latest version of node, it will also install the latest version of npm, and that worked for me.

This doesn't happen if you use nvm

"This doesn't happen if you use nvm"

Yeah, well, it doesn't happen if you use yarn either, but I'm not sure that's relevant.

I think that's irrelevant. NVM is node version manager, I experienced this when I used brew to install node, then I switched to nvm, since then I haven't experienced this yet. Check it out here if you don't know what it is https://github.com/nvm-sh/nvm#installing-and-updating

Was this page helpful?
0 / 5 - 0 ratings