38

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
  • 3,466
  • 7
  • 42
  • 73

6 Answers6

45

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
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • 4
    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 – Wayneio May 13 '18 at 13:45
  • @Wayneio `rm -r /usr/local/lib/node_modules/npm`? – Maxime Chéramy May 13 '18 at 13:55
  • 3
    @MaximeChéramy Thanks, its worked for. You saved my day. – Ajay Kurmi May 22 '18 at 11:26
  • Only ```rm -r /usr/local/lib/node_modules/npm``` was necessary for me. Thanks! – cactuschibre Jul 01 '19 at 22:01
13

(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
Pierluigi Vernetto
  • 1,954
  • 1
  • 25
  • 27
  • just a little change this ( http://nodejs.org/dist/latest/node-v11.4.0-linux-x64.tar.gz ) link has failed please use the new https://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 Jan 22 '20 at 10:55
  • It works for non centos also. I have ubuntu 14.0LTS and it worked. – Pradeep Singh Jan 22 '20 at 11:00
2

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.

ZontarZon
  • 584
  • 6
  • 15
2

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

Achint Sharma
  • 355
  • 4
  • 11
1

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

Thibault
  • 1,566
  • 15
  • 22
-2

For me, reinstalling npm worked:

npm install -g npm
George Stocker
  • 57,289
  • 29
  • 176
  • 237