node.js 如何重新安装损坏的 npm

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49748307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:45:36  来源:igfitidea点击:

How to Reinstall Broken npm

node.jsnpminstalluninstallreinstall

提问by CTS_AE

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

v9.11.1当我尝试使用 npm 时,我刚刚安装了节点,但不断收到以下错误:

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 npmbut it couldn't update itself, it just kept throwing the same error above.

我尝试这样做,npm i -g npm但它无法自行更新,它只是不断抛出上述相同的错误。

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

当我运行时,npm -v它说5.5.1目前似乎最新版本是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.

我尝试删除文件夹并在 node.js 安装程序上运行修复功能,但它没有替换模块。

回答by CTS_AE

Delete the Global NPM Folder

删除全局 NPM 文件夹

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

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

npm list -g

For Windows this will most likely be:

对于 Windows,这很可能是:

%AppData%\npm\node_modules

%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.

一旦有删除名为npm.

Download the Latest Release of NPM

下载最新版本的 NPM

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

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

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

获取 zip 并将其解压缩到您node_modules刚刚从中删除 npm 文件夹的文件夹中。

Rename the folder you extracted from the zip to npm

将您从 zip 中提取的文件夹重命名为 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 npmto update/reinstall npm without any warnings.

现在,您应该能够在npm i -g npm没有任何警告的情况下运行以更新/重新安装 npm。

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

如果您在 Windows 中遇到关于路径或文件名太长的错误,我发现这一步尤其重要。

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 npmsuccessfully.

您可能会收到以下错误。如果你确实去删除或移动它列出的文件,你应该能够npm i -g npm成功运行。

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.

回答by Willem van der Veen

Try this:

尝试这个:

npm install npm@latest -g

More info here.

更多信息在这里

回答by Gediminas

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.

对我来说,即使删除 npm 文件夹或重新安装 Node 也无济于事。将 Nodejs 和 npm 更新到我的 CentOS 6 的最新版本(Node 10.19,npm 6.14.3)后,当 Node 运行良好时,我的 npm 不断出现 libs 错误。

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

什么有效 -n节点版本管理器。使用此命令,我可以反转我的 npm 版本:n -p 6.13链接