乐闻世界logo
搜索文章和话题

How can I update npm on Windows?

5 个月前提问
3 个月前修改
浏览次数38

6个答案

1
2
3
4
5
6

要更新全局安装的npm工具,您可以执行以下步骤:

  1. 打开终端或命令提示符: 这是您将输入命令的地方。

  2. 检查是否有更新的工具: 使用如下命令可以查看全局安装的npm包:

    shell
    npm outdated -g --depth=0

    这将列出所有已经过时的全局npm包和它们的当前版本、需要更新的版本以及最新版本。

  3. 更新特定的npm工具: 如果您只想更新一个特定的工具,可以使用命令:

    shell
    npm update <工具名> -g

    其中<工具名>是您想要更新的工具的名称。

  4. 一次性更新所有全局工具: 如果您想要更新所有过时的工具,您可以使用命令:

    shell
    npm update -g

    这会更新所有全局安装的且有新版本可用的npm工具。

  5. 强制更新: 如果因为某些原因npm update命令没有更新到最新版本,您可以通过先卸载再安装的方式强制更新。例如:

    shell
    npm uninstall -g <工具名> npm install -g <工具名>@latest

    这样会确保您安装的是该工具的最新版本。

  6. 确认更新: 更新完成后,您可以确认工具是否更新到了最新版本:

    shell
    npm list -g <工具名>

    或者,如果您想查看所有全局安装的工具及其版本,可以运行:

    shell
    npm list -g --depth=0

示例

假设我们想更新全局安装的create-react-app工具。以下是更新流程的步骤:

  1. 打开您的命令行工具。

  2. 检查是否有可用的更新:

    shell
    npm outdated -g --depth=0

    这可能会告诉您create-react-app的当前版本和最新版本。

  3. 更新create-react-app到最新版本:

    shell
    npm update create-react-app -g
  4. 确认更新:

    shell
    npm list -g create-react-app

以上步骤将帮助您保持全局npm工具的最新状态。

2024年6月29日 12:07 回复

Note: The question is specifically asking how to upgrade npm, not Node.js. If you want to update Node.js over a CLI on windows, I recommend running winget upgrade -q NodeJS or use chocolatey for that.

What method should I choose to update NPM?

  • Node.js v16 or higher?
    • npm install -g npm
  • Node.js v14 or below?
    • Consider updating to latest LTS release of Node.js
    • npm-windows-upgrade

Upgrade with npm-windows-upgrade

Run PowerShell as Administrator

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

Note: if you run the Node.js installer, it will replace the Node.js version.

  • Upgrades npm in-place, where Node.js installed it.
  • Does not modify the default path.
  • Does not change the default global package location.
  • Allows easy upgrades and downgrades and to install a specific version.
  • A list of versions matched between NPM and Node.js (https://nodejs.org/en/download/releases/) - but you will need to download the Node.js installer and run that to update Node.js (https://nodejs.org/en/)

Upgrade with npm

shell
npm install -g npm

Note: some users still report issues updating npm with npm, but I haven't had that experience with v16+.

2024年6月29日 12:07 回复

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

2024年6月29日 12:07 回复

To update NPM, this worked for me:

  • Navigate in your shell to your node installation directory, eg C:\Program Files (x86)\nodejs
  • run npm install npm (no -g option)
2024年6月29日 12:07 回复

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

This should work 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

  1. Run this to update NPM:

    Run PowerShell as administrator

    shell
    npm i -g npm // This works

    I am not thinking this code actually upgrades your npm version below

    shell
    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:

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

Otherwise

You might need to set your proxy

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

2 possible errors

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

    shell
    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 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)

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

2024年6月29日 12:07 回复

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

shell
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

2024年6月29日 12:07 回复

你的答案