我试了一下:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

...但这并没有起作用。

如何在Windows上做到这一点?


当前回答

您可以使用这些命令:

npm cache clean
npm update -g [package....]

如果要从以前版本的node升级,则需要更新所有现有的全局包。 您还可以指定要更新的包名。

其他回答

为了更新NPM,这对我来说很有效:

在shell中导航到节点安装目录,例如C:\Program Files (x86)\nodejs 运行NPM install NPM(没有-g选项)

你可以使用Chocolatey,它是windows的包管理器(就像Debian Linux的apt-get)。

重新安装(您可能需要卸载以前安装的版本)

> choco install nodejs

更新到最新版本

> choco update nodejs

对于NPM

> choco update npm

像一些人一样,我需要组合多个答案,我还需要设置一个代理。

这对任何人都适用。我有零愿望运行EXE文件或MSI文件..卸载/重新安装,或手动删除文件和文件夹。这就是1999年的情况:P

运行这个来更新NPM: 以管理员身份运行PowerShell npm i -g npm // 我不认为这段代码实际上升级了你下面的npm版本 Set-ExecutionPolicy unlimited -Scope CurrentUser -Force NPM install -g NPM -windows-upgrade npm-windows-upgrade (由“罗伯特”回答提供)

运行这个来更新Node.js:

wget https://nodejs.org/download/release/latest/win-x64/node.exe -OutFile 'C:\Program Files (x86)\nodejs\node.exe'    (courtesy of BrunoLM answer)

如果你得到' wget:无法找到路径....的一部分**,见下文…向下滚动。阅读网络响应…它至少要通过防火墙/代理(如果你有一个或已经运行了代码,请通过…)

否则

您可能需要设置代理

npm config set proxy "http://proxy.yourcorp.com:811"    (yes, use quotes)

2个可能的错误

It cannot find path of the path solution "where.exe node" (courtesy of Lonnie Best Answer) E.g. if Node.js is NOT living in "Program Files (x86)" perhaps with where.exe, it is living in 'C:\Program Files\nodejs\node.exe'. wget https://nodejs.org/download/release/latest/win-x64/node.exe -OutFile 'C:\Program Files\nodejs\node.exe' Now perhaps it tries to upgrade but you get another error, "node.exe is being used by another process." Close /shutdown other consoles .. command prompts and PowerShell windows, etc. Even if you're using npm in a command prompt, close it.

npm -v (3.10.8)

Node -v (v6.6.0)

完成了。我得到了我想要的版本。

以管理员身份打开PowerShell。

要安装第一次,您可以使用这个小脚本下载最新的msi并运行它

$nodeLatest=((curl https://nodejs.org/download/release/latest/).Content | findstr x64.msi) -replace "<(.*?)>", "" -replace "\s+.+", "";
wget "https://nodejs.org/download/release/latest/$nodeLatest" -OutFile (join-path $env:TEMP node.msi); Start-Process (join-path $env:TEMP node.msi)

在未来的升级中,你可以下载node.exe并更新npm

wget https://nodejs.org/download/release/latest/win-x64/node.exe -OutFile 'C:\Program Files\nodejs\node.exe'
npm i -g npm

现在你应该有了最新的节点和npm。


我更进一步,决定为Windows实现一个nvm。

https://github.com/brunolm/nvm

Install-Module -Name power-nvm

nvm install latest
nvm default latest

好了,朋友们,我阅读了(在Windows上尝试了)之前所有的东西,所有这些答案都有自己的缺点。

关于更新Node.js的最佳方法(至少对我来说),请访问https://nodejs.org/en/ 然后下载最后一个版本,并将其安装到您在1分钟内安装前一个版本的相同文件夹中,这样就完成了。您不需要删除任何旧文件。

然后更新npm输入cmd: npm install——save latest-version