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

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

How do I update npm on Windows?

windowsnode.jsnpm

提问by Sam Mikes

When I install node 0.10.33 from the msi ( http://nodejs.org/download/), I get an old version of npm (1.4.28). How can I upgrade npm on Windows?

当我从 msi ( http://nodejs.org/download/)安装节点 0.10.33 时,我得到了一个旧版本的 npm (1.4.28)。如何在 Windows 上升级 npm?

npm install -g npm

npm install -g npm

does not work; I still have the old npm.

不起作用;我还有旧的 npm。

回答by Sam Mikes

You need to follow the Windows upgrade instructions ( https://docs.npmjs.com/try-the-latest-stable-version-of-npm)

您需要遵循 Windows 升级说明 ( https://docs.npmjs.com/try-the-latest-stable-version-of-npm)

tl;dr - npm -g install npmdoeswork, but the old version of npm is still in your PATH.

tl;dr -npm -g install npm确实有效,但旧版本的 npm 仍在您的 PATH 中。

To fix this, do one of these:

要解决此问题,请执行以下操作之一:

Option 2: remove both of

C:\Program Files (x86)\nodejs\npm

C:\Program Files (x86)\nodejs\npm.cmd

选项2:删除两者

C:\Program Files (x86)\nodejs\npm

C:\Program Files (x86)\nodejs\npm.cmd

Or

或者

Option 3: Open cmd.exe as administrator, navigate to C:\Program Files (x86)\nodejs and then run the installation without -g:

npm install npm@latest

选项 3:以管理员身份打开 cmd.exe,导航到 C:\Program Files (x86)\nodejs,然后不带 -g 运行安装:

npm install npm@latest

*There is an npm package that automate this Option 3:

*有一个 npm 包可以自动执行此选项 3:

npm install -g npm-win-upgrade

npm install -g npm-win-upgrade

回答by Felix Rieseberg

We at Microsoft Open Source wrote a small tool to automate the process outlined above. You can find it hereor just install and run it by executing the following in an elevated command prompt / PowerShell:

我们在 Microsoft Open Source 编写了一个小工具来自动化上述过程。您可以在此处找到它,也可以通过在提升的命令提示符/PowerShell 中执行以下命令来安装和运行它:

npm install -g npm-windows-upgrade
npm-windows-upgrade

回答by Vivek Maru

There May be many ways to update your npmin Windows

可能有很多方法可以npm在 Windows 中更新您的

Way 1

方式一

  1. Open you power shell as Administrator and run following commands step by step.
  2. Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
  3. npm install -g npm-windows-upgrade
  4. npm-windows-upgrade
  5. Here you can select your preferred version of npm
  6. Ready to go !
  1. 以管理员身份打开您的电源外壳并逐步运行以下命令。
  2. Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
  3. npm install -g npm-windows-upgrade
  4. npm-windows-upgrade
  5. 在这里你可以选择你喜欢的 npm 版本
  6. 准备好出发 !

Way 2

方式二

  1. You can simply upgrade to the latest npmversion with following command
  2. npm install npm@latest -g
  3. or if you want a specific version of npmsimply run
  4. npm install npm@{version} -gfor example npm install [email protected] -g
  1. 您可以npm使用以下命令简单地升级到最新版本
  2. npm install npm@latest -g
  3. 或者如果你想要一个特定版本的npm简单运行
  4. npm install npm@{version} -g例如 npm install [email protected] -g

Now simply run npm --versionor npm -vto know your current version of npm

现在只需运行npm --versionnpm -v了解您当前的 npm 版本

3.3.12is my preferred version of npmthat best suits for approximately every package.

3.3.12是我最喜欢的版本npm,最适合几乎每个包裹。

回答by Sudhanshu Mishra

So, TL;DR; this worked:

所以,TL; DR; 这有效:

  1. Uninstall node

  2. From a command prompt, run where npm

  3. If you had npm installed via chocolatey, you'll see a path like C:\ProgramData\chocolatey\bin\npm.exe - DELETE it!
  4. Now install NodeJS using the appropriate MSI from https://nodejs.org/en/download/
  5. In your favourite shell, type npm --version- this should now echo the version of NPM that came with NodeJS (at the time of this writing, that version is 3.10.10)
  1. 卸载节点

  2. 从命令提示符运行 where npm

  3. 如果你通过 Chocolatey 安装了 npm,你会看到像 C:\ProgramData\chocolatey\bin\npm.exe 这样的路径 - 删除它!
  4. 现在使用来自https://nodejs.org/en/download/的相应 MSI 安装 NodeJS
  5. 在您最喜欢的 shell 中,输入npm --version- 现在应该与 NodeJS 附带的 NPM 版本相呼应(在撰写本文时,该版本为 3.10.10)

This is what worked for me (goofy me!) I had (age ago) installed npm via chocolatey, which created a chocolatey initiated npm.exe in C:\ProgramData\chocolatey\bin\npm.exe. This was npm version 1.4.9 and wouldn't update no matter what one did including uninstall and reinstall NodeJs.

这对我有用(愚蠢的我!)我(很久以前)通过巧克力安装了 npm,它在 C:\ProgramData\chocolatey\bin\npm.exe 中创建了一个巧克力启动的 npm.exe。这是 npm 版本 1.4.9,无论做什么,包括卸载和重新安装 NodeJs,都不会更新。

=======

========

EDIT: Better way to install node and npm

编辑:安装节点和 npm 的更好方法

As of today (27/06/2017), the best way to install and manage node and npm is to install nvm (Node Version Manager) as explained here: https://github.com/coreybutler/nvm-windows. Once you have nvm, installing any node version is super easy:

截至今天(27/06/2017),安装和管理 node 和 npm 的最佳方法是安装 nvm(节点版本管理器),如下所述:https: //github.com/coreybutler/nvm-windows。拥有 nvm 后,安装任何节点版本都非常简单:

  1. Open your favourite console (CMD, Cmder, PowerShell)
  2. Type nvm install 6.10.2(to install node version 6.10.2)
  3. To see the currently active version, type nvm list. This prints something like below:
  1. 打开你最喜欢的控制台(CMD、Cmder、PowerShell)
  2. 键入nvm install 6.10.2(安装节点版本 6.10.2)
  3. 要查看当前活动的版本,请键入nvm list。这会打印如下内容:

  6.9.3   
* 6.9.2 (Currently using 64-bit executable)
  6.9.1
  6.10.2

回答by Ganesh Apune

You need to follow the Windows upgrade instructions

您需要按照 Windows 升级说明进行操作

https://www.npmjs.com/package/npm-windows-upgrade

https://www.npmjs.com/package/npm-windows-upgrade

First, ensure that you can execute scripts on your system by running the following command from an elevated PowerShell. To run PowerShell as Administrator, click Start, search for PowerShell, right-click PowerShell and select Run as Administrator.

首先,通过从提升的 PowerShell 运行以下命令,确保您可以在系统上执行脚本。要以管理员身份运行 PowerShell,请单击开始,搜索 PowerShell,右键单击 PowerShell 并选择以管理员身份运行。

  1. Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
  2. npm install --global --production npm-windows-upgrade
  3. npm-windows-upgrade
  1. Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force
  2. npm install --global --production npm-windows-upgrade
  3. npm-windows-upgrade

Want to just install the latest version? 1.npm-windows-upgrade --npm-version latest

只想安装最新版本?1.npm-windows-upgrade --npm-version 最新

回答by user11582062

all npm install commands were getting stuck and will throw error (behind proxy error) after sometime. setting up proxy was not helping. This is what I did.

所有 npm install 命令都卡住了,一段时间后会抛出错误(在代理错误之后)。设置代理没有帮助。这就是我所做的。

Had 8.11 nodeJS

有 8.11 nodeJS

  1. Uninstalled NodeJS from Programs & Features with the uninstaller.
  2. Reboot (or you probably can get away with killing all node-related processes from Task Manager).
  3. Look for these folders and remove them (and their contents) if any still exist. Depending on the version you installed, UAC settings, and CPU architecture, these may or may not exist: ? C:\Program Files (x86)\Nodejs ? C:\Program Files\Nodejs ? C:\Users{User}\AppData\Roaming\npm (or %appdata%\npm) ? C:\Users{User}\AppData\Roaming\npm-cache (or %appdata%\npm-cache) ? C:\Users{User}.npmrc (and possibly check for that without the . prefix too)
  4. Check your %PATH% environment variable to ensure no references to Nodejs or npm exist.
  5. If it's still not uninstalled, type where node at the command prompt and you'll see where it resides -- delete that (and probably the parent directory) too.
  6. Reboot, for good measure.
  1. 使用卸载程序从程序和功能中卸载 NodeJS。
  2. 重新启动(或者您可能可以从任务管理器中杀死所有与节点相关的进程)。
  3. 查找这些文件夹并删除它们(及其内容)(如果仍然存在)。根据您安装的版本、UAC 设置和 CPU 架构,这些可能存在也可能不存在:C:\Program Files (x86)\Nodejs ? C:\Program Files\Nodejs ?C:\Users{User}\AppData\Roaming\npm(或 %appdata%\npm)?C:\Users{User}\AppData\Roaming\npm-cache(或 %appdata%\npm-cache)?C:\Users{User}.npmrc (也可能检查没有 . 前缀)
  4. 检查您的 %PATH% 环境变量以确保不存在对 Nodejs 或 npm 的引用。
  5. 如果它仍未卸载,请在命令提示符下键入 where node,您将看到它所在的位置——也删除它(可能还有父目录)。
  6. 重新启动,很好的措施。

Installed 6.11 nodejs then it worked.

安装了 6.11 nodejs 然后它工作了。

回答by Ben Thomson

So none of the previous answers solved the issue for me so I thought I would post my specific solution, which I managed to figure out by going through all the other answers so they were really helpful.

所以之前的答案都没有为我解决这个问题,所以我想我会发布我的具体解决方案,我通过浏览所有其他答案设法弄清楚,所以它们真的很有帮助。

My issue was because I had used chocolatey to install node and possibly npm (looking at chocolatey site now I shouldn't have done that).

我的问题是因为我使用了 Chocolatey 来安装 node 和可能的 npm(现在查看 Chocolatey 站点我不应该这样做)。

To solve the issue I simply had to run the relevant choco uninstall commands for npm and node and then everything switched to the other version of node which I had also installed using the node msi (from node's website).

为了解决这个问题,我只需要为 npm 和 node 运行相关的 choco 卸载命令,然后一切都切换到我也使用 node msi(来自 node 的网站)安装的其他 node 版本。

I hope that helps anyone else that may have gone down the same path as me.

我希望它可以帮助任何可能和我一样走上同一条路的人。

回答by ravthiru

Use npm-windows-upgradetool to simply upgrade. Steps are provided in the link.

使用npm-windows-upgrade工具简单升级。链接中提供了步骤。

回答by hamzox

I tried almost every answer but none of them works my way.
Neither npm-windows-upgradeworked nor did the npm install npm@latestetc etc worked.
For people like me I will suggest you guys downloading the latest installer from Node.js website, let the existing version of node directory be on it's place and without changing anything just install the msiinstaller and you will end up with an upgraded version of node.
My case was upgrading node from 6to 8.9.3

我尝试了几乎所有的答案,但没有一个能按我的方式工作。
无论是NPM-Windows的升级工作也没有了故宫故宫安装最新的@等等等等工作。
对于像我这样的人,我建议你们从Node.js 网站下载最新的安装程序,让现有版本的 node 目录放在它的位置,不做任何更改,只需安装msi安装程序,你最终会得到升级版本的 node .
我的情况是将节点从6升级到8.9.3

回答by kayleeFrye_onDeck

I did something similar to Sam Mikes. I'm only sharing this because I couldn't get either of his solutions to work on my rig. After a bit of playing around, this is what worked for me:

我做了一些类似于 Sam Mikes 的事情。我只是分享这个,因为我无法让他的任何一个解决方案在我的装备上工作。经过一番玩耍后,这对我有用:

  1. Remove NPM/Node from your Environment Variables, both for user and system.
  2. Close your open console if you have one open, then open a console as administrator
  3. Change directories to %USERPROFILE%-- that's a window's environment variable that takes you to C:\Users\CurrentlyLoggedInUser
  4. Run from the console in %USERPROFILE%, "C:\Program Files\nodejs\npm" install npm -g(you might have yours in the x86 folder)
  5. If this solution would work for you, step 4 would have worked, and npm -vwill show a an up-to-date version
  6. Re-add Node (in Program Files)/NPM (in App Data -- the one installed in Program Files should be the old one) to your user and system environment variables
  1. 从用户和系统的环境变量中删除 NPM/Node。
  2. 如果您打开控制台,请关闭打开的控制台,然后以管理员身份打开控制台
  3. 将目录更改为%USERPROFILE%-- 这是一个窗口的环境变量,它会将您带到C:\Users\CurrentlyLoggedInUser
  4. 从 %USERPROFILE% 中的控制台运行"C:\Program Files\nodejs\npm" install npm -g(您的 x86 文件夹中可能有您的)
  5. 如果此解决方案适合您,则第 4 步将有效,npm -v并将显示最新版本
  6. 将 Node(在 Program Files 中)/NPM(在 App Data 中——安装在 Program Files 中的应该是旧的)添加到您的用户和系统环境变量中

Note: I've used the Microsoft automated script in the past to fix this, but only because I didn't realize how easy it would be to do it myself.

注意:我过去曾使用 Microsoft 自动化脚本来解决此问题,但这只是因为我没有意识到自己完成此操作是多么容易。