How to Reinstall Broken npm

83,525

Solution 1

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.

Solution 2

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.

Solution 3

Try this:

npm install npm@latest -g

More info here.

Solution 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
Share:
83,525
CTS_AE
Author by

CTS_AE

https://about.me/codyswartz/

Updated on November 01, 2021

Comments

  • CTS_AE
    CTS_AE over 2 years

    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.