39

I just installed node v9.11.1 when I try to use npm I keep getting the following error:

npm WARN npm npm does not support Node.js v9.11.1
npm WARN npm You should probably upgrade to a newer version of node as we
npm WARN npm can't make any promises that npm will work with this version.
npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8.
npm WARN npm You can find the latest version at https://nodejs.org/

I tried to do npm i -g npm but it couldn't update itself, it just kept throwing the same error above.

When I run npm -v it says 5.5.1 it seems the latest version is currently 5.8.0.

I tried deleting the folder and running the repair function on the node.js installer, but it didn't replace the module.

CTS_AE
  • 12,987
  • 8
  • 62
  • 63
  • I ended up with a broken installation of npm as well. In my case, I was getting the following error when attempting to run any npm command: `TypeError: Class extends value undefined is not a constructor or null at Object. (/usr/local/lib/node_modules/npm/node_modules/socks-proxy-agent/dist/agent.js:114:44) at Module._compile (node:internal/modules/cjs/loader:1101:14) [...]` – Tweek Nov 13 '21 at 20:05

7 Answers7

50

Delete the Global NPM Folder

https://stackoverflow.com/a/5926706/349659

npm list -g

For Windows this will most likely be:

%AppData%\npm\node_modules

You can paste that into a folder's address bar and it will take you there.

Once there delete the folder named npm.

Download the Latest Release of NPM

https://github.com/npm/cli/releases/latest

Grab the zip and unzip it to your node_modules folder that you just deleted the npm folder from.

Rename the folder you extracted from the zip to npm

If you get any warnings about the file path or name being too long skip the warnings.

Update for Good Luck

Now you should be able to run npm i -g npm to update/reinstall npm without any warnings.

I find this step especially important if you had errors in Windows about the path or file name being too long.

You may get the following errors. If you do go and delete or move the files it has listed and you should be able to run npm i -g npm successfully.

npm ERR! Refusing to delete C:\Program Files\nodejs\npx.cmd: is outside C:\Program Files\nodejs\node_modules\npm and not a link
npm ERR! File exists: C:\Program Files\nodejs\npx.cmd
npm ERR! Move it away, and try again.

npm ERR! Refusing to delete C:\Program Files\nodejs\npm.cmd: is outside C:\Program Files\nodejs\node_modules\npm and not a link
npm ERR! File exists: C:\Program Files\nodejs\npm.cmd
npm ERR! Move it away, and try again.
CTS_AE
  • 12,987
  • 8
  • 62
  • 63
  • if `npm --version` throws an error like "Error [ERR_REQUIRE_ESM]: Must use import to load ES Module" this answer doesn't help. but I guess OP got a clean `npm` error – electblake Jan 28 '22 at 19:10
20

Somehow I had a broken installation of npm so reinstalling it with itself (e.g. npm install -g npm or similar) would not work.

The npm readme actually defines a very simple way to install npm again using this curl command:

curl -qL https://www.npmjs.com/install.sh | sudo sh

This installation script actually removes any existing npm installations, then installs the latest version for you.

Zander
  • 2,471
  • 3
  • 31
  • 53
9

Try this:

npm install npm@latest -g

More info here.

Willem van der Veen
  • 33,665
  • 16
  • 190
  • 155
  • 5
    I tried to do that but because npm was broken I was unable to run npm to install/reinstall npm. – CTS_AE Apr 13 '18 at 23:59
  • 1
    In my case, this helped. @cts_ae: Maybe you need to fix the cache first, try `npm cache verify` or `npm cache clean` – Matt Jun 16 '20 at 10:05
4

Try this command to reinstall npm

npm install -g npm-reinstall

If you are using a linux distribution add sudo

sudo npm install -g npm-reinstall
3

Since I am all about one-liners, let me make this very damn easy for you.

Open your GitBash or Cygwin(I think both GitBash and Cygwin stores the Windows pre-defined Environment Variables and paths in their storage, so this becomes way easy), on any path, and run this one-liner from there:

rm -rf $APPDATA/npm; npm install -g npm@latest; npm list -g;

rm -rf $APPDATA/npm; - this will purge the old global npm folder completely.

npm install -g npm@latest; - this will install the latest version of NPM available(so no need to install minor versions after this ;)).

npm list -g; - this will show you details of your NPM global profile.

If anyone insists on doing this in PowerShell then here's the way:

$env:Path += ";C:\Program Files\nodejs\"
cmd /c 'del /s /q /f %APPDATA%\npm && rd /s /q %APPDATA%\npm'; npm install -g npm@latest; npm list -g;

$env:Path += ";C:\Program Files\nodejs\" - Ensures that Nodejs is added to Path for atleast running PowerShell session. (Caution: Run this only once, as long as no errors.).

cmd /c 'del /s /q /f %APPDATA%\npm && rd /s /q %APPDATA%\npm'; - Ensures that the necessary step of fully purging the %APPDATA%\npm folder is done with measure.

Vicky Dev
  • 1,893
  • 2
  • 27
  • 62
  • 1
    The original problem was that npm was broken thus you cannot use it as a dependency to install itself. I do wonder if this would have worked though since npm wasn't completely borked [looking at my original problem]. I'm unable to reproduce this now, but hopefully this is helpful for others that come along. ++ For automating the delete – CTS_AE Mar 03 '22 at 00:16
  • @CTS_AE Yep, glad to help out with different approach – Vicky Dev Mar 03 '22 at 00:38
1
  1. Navigate to and delete %AppData%\npm\node_modules\npm

  2. Use npm cli repo's curl script to automatically clean previous npm install and re-install the latest version:

curl -qL https://www.npmjs.com/install.sh | sh

repo: npm github repo

J Boon
  • 39
  • 3
  • 1
    on mac, I was unable to find the old `npm` path. Ran the above command, it auto removed the old one and installed `npm@latest` – Ijaz Ur Rahim May 16 '23 at 11:14
0

For me even deleting npm folder or reinstalling Node didn't help. After updating Nodejs and npm to latest version (Node 10.19, npm 6.14.3) of my CentOS 6, I've got constant libs error with npm while Node was fine.

What's worked - n Node version manager. With this command I could reverse my npm version: n -p 6.13 link

Gediminas
  • 124
  • 2
  • 6