npm update broke npm

To those who used google to find this, you may be tempted to install via curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash - and then installing with sudo apt install nodejs.

However, I somehow ran into this issue regardless. Please keep in mind that npm@6 dropped support for node@<=4, and that is a contributing factor here. If you want to be sure that everything is installed at the latest, correct versions, I very highly recommend installing through nvm.

Via the nvm instructions on their GitHub: You can add the install script with

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

Then you can start using nvm. You will likely have to restart your terminal, so after installation, exit the terminal, start it up again, and check that nvm is installed with nvm --version.

If everything goes well, you can install any specific version of node with npm in tow. The latest stable version of node as of writing this is 10.15.3, so

 nvm install 10.15.3

And of course, if you need help, nvm --help has a list of options.


You probably have npm installed twice, one is in /usr/local/bin and the other in /usr/bin.

First, you can try to remove the npm module that has been installed by upgrading npm. Try to run this:

  • rm -r /usr/local/lib/node_modules/npm
  • /usr/bin/npm uninstall npm

Once you have a running version of npm, install a more recent version of node before upgrading npm. Then, remove the version of your linux distribution.

If the first solution doesn't work, another approach is to install a recent version of node (without using npm of course):

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

(solution for centos....I assume it would work also on ubuntu):

to clean up completely my centos machine, I have additionally done the following - my user is "centos" and my home is /home/centos:

sudo rm -rf /usr/local/bin/npm 
sudo rm -rf /usr/local/bin/npx
sudo rm -rf /usr/lib/node_modules/
sudo rm -rf /usr/bin/npm
sudo rm -r /usr/local/lib/node_modules/
sudo rm -rf /usr/local/bin/node
sudo rm -rf /usr/bin/npm
sudo rm -rf /usr/lib/node_modules/
rm -rf /home/centos/.npm/
rm -rf /home/centos/node*
rm -rf /home/centos/.node-gyp/
sudo rm -rf /root/.npm/
sudo rm /usr/bin/node
sudo rm -rf /usr/local/include/node

only at this point I reinstalled again:

wget http://nodejs.org/dist/latest/node-v11.4.0-linux-x64.tar.gz
sudo tar --strip-components 1 -xzvf node-v* -C /usr/local

and things are working again:

node --version
v11.4.0
npm --version
6.4.1

If you are using nvm to install npm and node, try this solution.

  1. Get to know where exactly is the currently used node and npm is installed:

    which node

    In my case, it was /home/ubuntu/.nvm/versions/node/

  2. Now, delete all the versions of node using:

    sudo rm -rf /home/ubuntu/.nvm/versions/node/

  3. You can now use nvm to install your required version of node and npm.

    nvm install 4.9.1

Tags:

Linux

Node.Js

Npm