node.js 如何在 Windows 上更新 npm?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18412129/
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 15:24:39  来源:igfitidea点击:

How can I update npm on Windows?

windowsnode.jsnpmpackage-managers

提问by Jatin

I tried this:

我试过这个

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

...but it didn't work.

......但它没有用。

How do I do this on Windows?

如何在 Windows 上执行此操作?

回答by Robert Baker

This is the new best way to upgrade npmon Windows.

这是在 Windows升级npm 的最佳新方法。

Run PowerShell as Administrator

以管理员身份运行 PowerShell

Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
npm install -g npm-windows-upgrade
npm-windows-upgrade

Note: Do not run npm i -g npm. Instead use npm-windows-upgradeto update npm going forward. Also if you run the NodeJS installer, it will replace the node version.

注意:不要运行npm i -g npm. 而是用于npm-windows-upgrade更新 npm。此外,如果您运行 NodeJS 安装程序,它将替换节点版本。

回答by josh3736

Downloadand run the latest MSI. The MSI will update your installed node and npm.

下载并运行最新的 MSI。MSI 将更新您安装的节点和 npm。

回答by meetar

To update NPM, this worked for me:

要更新 NPM,这对我有用:

  • Navigate in your shell to your node installation directory, eg C:\Program Files (x86)\nodejs
  • run npm install npm(no -goption)
  • 在你的 shell 中导航到你的节点安装目录,例如 C:\Program Files (x86)\nodejs
  • 运行npm install npm(无-g选项)

回答by Tom Stickel

Like some people, I neededto combine multiple answers, and I also needed to set a proxy.

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

This shouldwork for anyone. I have zero desire to run an EXE file or MSI file .. uninstall/ reinstall, or manually delete files and folders. That is so 1999 :P

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

  1. Run this to update NPM:

    Run PowerShell as administrator

    npm i -g npm    // This works
    

    I am notthinking this code actually upgrades your npm version below

    Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
    npm install -g npm-windows-upgrade
    npm-windows-upgrade
    
    (courtesy of "Robert" answer)
    
  1. 运行此命令以更新 NPM:

    管理员身份运行 PowerShell

    npm i -g npm    // This works
    

    认为这段代码实际上升级了下面的 npm 版本

    Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
    npm install -g npm-windows-upgrade
    npm-windows-upgrade
    
    (courtesy of "Robert" answer)
    

Run this to update Node.js:

运行它来更新 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)

If you get `wget : Could not find a part of the path .... "**, see below ...scroll down. Reading Web Response... It's at least punching through the firewall /proxy (if you have one or have already ran the code get through ...

如果你得到 `wget : 找不到路径的一部分......“**,见下文......向下滚动。阅读 Web 响应......它至少穿透了防火墙/代理(如果你有一个或者已经运行代码通过...

Otherwise

除此以外

You might need to set your proxy

您可能需要设置代理

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

2 possible errors

2个可能的错误

  1. 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'
    
  2. Now perhaps it tries to upgrade but you get anothererror, "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.
  1. 它找不到路径解决方案“where.exe node”的路径(由 Lonnie Best Answer 提供)

    例如,如果 Node.js 不存在于“Program Files (x86)”中,也许与 where.exe 一起存在,则它存在于“C:\Program Files\nodejs\node.exe”中。

    wget https://nodejs.org/download/release/latest/win-x64/node.exe -OutFile 'C:\Program Files\nodejs\node.exe'
    
  2. 现在它可能会尝试升级,但您会收到另一个错误,“node.exe 正在被另一个进程使用”。

    • 关闭 /shutdown 其他控制台 .. 命令提示符和 PowerShell 窗口等。即使您在命令提示符中使用 npm,也要关闭它。

npm -v (3.10.8)

npm -v (3.10.8)

node -v ( v6.6.0)

节点 -v ( v6.6.0)

DONE. I'm at the version that I want.

完毕。我在我想要的版本。

回答by Ahmad M

You can use Chocolateywhich is a package manager for windows (like apt-get for Debian Linux).

您可以使用Chocolatey,它是 Windows 的包管理器(例如 Debian Linux 的 apt-get)。

Install fresh (you might need to uninstall previously installed versions)

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

> choco install nodejs

Update to the latest version

更新到最新版本

> choco update nodejs

and for npm

对于 npm

> choco update npm

回答by Juan David

You can update your npm to the latest stable version with the following command:

您可以使用以下命令将 npm 更新到最新的稳定版本:

 npm install npm@latest -g

Use PowerShell to run it. This command doesn't need windows administrator privileges and you can verify the result with npm -v

使用 PowerShell 运行它。此命令不需要 Windows 管理员权限,您可以使用以下命令验证结果npm -v

回答by Will Lopez

The previous answers will work installing a new version of Node.js (probably the best option), but if you have a dependency on a specific Node.js version then the following will work: "npm install npm -g". Verify by running npm -v before and after the command.

之前的答案将适用于安装新版本的 Node.js(可能是最好的选择),但如果您依赖于特定的 Node.js 版本,那么以下方法将起作用:“npm install npm -g”。通过在命令前后运行 npm -v 进行验证。

Enter image description here

在此处输入图片说明

回答by bohem.be

This works fine for me to update npm on Windows 7 x64:

这对我在 Windows 7 x64 上更新 npm 很有效:

  • Windows start
  • All Programs
  • Node.js
  • Node.js command prompt (alternative click)
  • Run as administrator

    $ npm -g install npm

  • remove C:\Program Files\nodejs\npm.cmdthe new npm will be at C:\Users\username\appdata\roaming\npm\npm.cmd

  • 视窗启动
  • 所有程序
  • 节点.js
  • Node.js 命令提示符(替代单击)
  • 以管理员身份运行

    $ npm -g install npm

  • 删除C:\Program Files\nodejs\npm.cmd新的 npm 将在C:\Users\username\appdata\roaming\npm\npm.cmd

Hope this helps.

希望这可以帮助。

回答by BrunoLM

Open PowerShell as administrator.

以管理员身份打开 PowerShell。

To install a first time you can use this small script to download the latest msiand run it

第一次安装你可以使用这个小脚本下载最新的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)

On future upgrades you can download just node.exeand update npm with

在未来的升级中,您可以只下载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

You should now have the latest node and npm.

您现在应该拥有最新的节点和 npm。



I went a little further and decided to implement a nvmfor Windows.

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

https://github.com/brunolm/nvm

https://github.com/brunolm/nvm

Install-Module -Name power-nvm

nvm install latest
nvm default latest

回答by xgqfrms

Use Upgrade npm on Windows

在 Windows 上使用升级 npm

This is the official document for a user to upgrade npm on Windows!

这是用户在 Windows 上升级 npm 的官方文档!

Here is my screenshot!

这是我的截图!

Enter image description here

在此处输入图片说明