npm update broke npm

53,736

Solution 1

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 2

(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

Solution 3

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.

Solution 4

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

Solution 5

Other answers didn't work for me on Ubuntu and ended up in a dead end, with a broken npm or unable to reinstall/update npm.

The radical solution I used :

1/ Remove all traces of node. Follow this page, using the remove.sh script at the bottom : http://kselax.ru/en/npm-errors/

2/ Then reinstall from scratch nodejs + npm using the latest install script : https://github.com/nodesource/distributions/blob/master/README.md

Share:
53,736
Wayneio
Author by

Wayneio

Updated on July 22, 2021

Comments

  • Wayneio
    Wayneio almost 3 years

    I just followed this guide to update npm (as my nodered camera module wasn't working) and ran

    npm install -g npm
    

    but now my npm install seems completely broken. If I just type

    npm
    

    or

    npm update
    

    I get

    /usr/local/lib/node_modules/npm/bin/npm-cli.js:79 let notifier = require('update-notifier')({pkg}) ^^^

    SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode

    I've tried

    sudo apt-get remove npm
    sudo apt-get install npm
    

    but the reinstall didn't help.

    I think my node version needs upgrading from v4.8.2 but I thought that was only possible with npm?

  • Wayneio
    Wayneio about 6 years
    running: /usr/bin/npm uninstall npm gives me: /usr/bin/npm: No such file or directory running: /usr/local/bin/npm uninstall npm gives me: /usr/local/lib/node_modules/npm/bin/npm-cli.js:79 let notifier = require('update-notifier')({pkg}) ^^^ SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
  • Maxime Chéramy
    Maxime Chéramy about 6 years
    @Wayneio rm -r /usr/local/lib/node_modules/npm?
  • Ajay Kurmi
    Ajay Kurmi almost 6 years
    @MaximeChéramy Thanks, its worked for. You saved my day.
  • cactuschibre
    cactuschibre almost 5 years
    Only rm -r /usr/local/lib/node_modules/npm was necessary for me. Thanks!
  • Pradeep Singh
    Pradeep Singh over 4 years
    just a little change this ( nodejs.org/dist/latest/node-v11.4.0-linux-x64.tar.gz ) link has failed please use the new nodejs.org/en/download find linux x64 and download manually( because we can't provide an automated way for it yet). Then all is well :-)
  • Pradeep Singh
    Pradeep Singh over 4 years
    It works for non centos also. I have ubuntu 14.0LTS and it worked.