How to Reinstall Broken npm
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.
Try this:
npm install npm@latest -g
More info here.
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.