ChatGPT解决这个技术问题 Extra ChatGPT

How can I update npm on Windows?

This question's answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions.

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?

The usual procedure for updating software that doesn't have built-in autoupdaters is to download and install the latest version. Have you tried that?
@Juhana I was thinking maybe there was some way to do it via npm as mentioned in that link.
You can do it with Chocolatey. See my answer here for more info: stackoverflow.com/a/19915418/373655
So how are you using sudo on windows? superuser.com/questions/42537/…
sudo does not work on Windows...

R
Robert Baker

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 using chocolatey for that.

What method should I choose to update NPM?

Node.js v16 or higher? npm install -g npm

npm install -g npm

Node.js v14 or below? Consider updating to latest LTS release of Node.js npm-windows-upgrade

Consider updating to latest LTS release of Node.js

npm-windows-upgrade

Upgrade with npm-windows-upgrade

Run PowerShell as Administrator

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

npm install -g npm

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


Was having issues getting mine to upgrade, this script worked perfectly. Thanks.
Microsoft recommends using this in their nodejs-guidlines repo on github. It also suggests tools for managing node versions.
This worked great for npm. thought I would mention, as the question itself is for updating node and npm, that to get both updated I had to uninstall / reinstall node, and run this upgrader as well (not sure if the node installer would have done it by itself as I did the upgrader first)
and what about node itself?
I followed the instruction, but when running "npm-windows-upgrade" I get the following error: npm-windows-upgrade : The term 'npm-windows-upgrade' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
j
josh3736

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


Note 32 and 64 bit MSIs. Do not just click on "Windows Installer" link - that's 32 bit. Check where your current nodejs resides, in "Program Files" or on "Program Files (x86)". The "x86" means 32-bit. See the comments below about "old version was installed in a different directory".
As of now (November 2014) this is will get you the latest node (0.10.33) but not the latest npm -- you will get npm 1.4.28. To update to a modern (2.x.x) npm on Windows, follow the instructions here: github.com/npm/npm/wiki/Troubleshooting#upgrading-on-windows
The node installer contains the latest version of npm that was available at the time of the node release. (on the 1.x branch)
I would recommend uninstalling your current node version from "Programs and Features" first...
(Feb 2018; it must have gotten easier!) I've been working with Node and npm for a couple years. I get Node msi's from nodejs.org. Each msi I've used has upgraded from an older Node version without complaint or notice. Recent Node msi's have included an npm, though sometimes an old npm. docs.npmjs.com/troubleshooting/… says npm install -g npm works. I've done it; I haven't had difficulty. Very recently, I upgraded Node and got the current npm, too.
G
Gal Margalit

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)


This is actually one of the recommended approaches on Windows: github.com/npm/npm/wiki/Troubleshooting#upgrading-on-windows
This worked for me after I had updated node via the accepted answer.
@singe3 The command prompt must be elevated (started as Admin) to be able to modify C:\Program Files. I don't really like this method because "npm install -g" will try to install under Program Files, thus the cmd prompt must always be run elevated to install other global packages. Option 2 in grenade's link is better IMHO: delete Program Files\nodejs\npm.cmd so it'll use the user's local npm.
I needed to use npm install npm@latest
@TryingToImprove You should uninstall node, delete program files\node and %appdata%\node. ReInstall node. Then use npm-windows-upgrade (see my answer). npm-windows-upgrade is the best solution and does not have the pitfalls like the other answers here. The pitfall you have is now 2 npm versions on your system and depending on if the npmrc file, you could have multiple global packages and inconsistency. Like updating a global package but still getting the old version...
T
Tom Stickel

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

Run this to update NPM: Run PowerShell as administrator npm i -g npm // This works I am not thinking 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)

Run this to update 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 ...

Otherwise

You might need to set your proxy

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

2 possible errors

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)

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


My node is in c:\Program Files\nodejs
This is the only answer that could still work (others looks pretty old and deprecated) and thanks god it works.
After deleted previous version of nodejs instance( like from program files, user data etc). It was giving me error like a previous version of nodejs is already install when i was trying to install 8.x version of nodejs. It solved my problem.
This will work only for node version greater than 8.0.0, mine is 6.x.x. It is not working for me. I wanted upgrade on ADO agent
J
Juan David

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

 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


Simplest and best answer for updating npm
If you have nodejs installed, there are two version of npm installed on Windows. Running npm install npm@latest -g only update the global ( -g ) installed one ( %appdata%\npm\ ). To update npm that comes with nodejs ( %ProgramFiles%\nodejs\node_modules\npm ) you can download the .msi installer from nodejs.org/en and run it. Other official methods to upgrade npm: github.com/npm/npm/wiki/Troubleshooting#upgrading-on-windows
Worked for me. Thank you :)
Worked for me :)
This seems like the best answer because of its simplicity.
A
Ahmad M

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

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

> choco install nodejs

Update to the latest version

> choco update nodejs

and for npm

> choco update npm

choco update npm is deprecated. Use npm-windows-upgrade to update npm. github.com/felixrieseberg/npm-windows-upgrade
@Robert choco update command is deprecated but we still can use choco upgrade command
@Aqib the package npm is deprecated in chocolatey. See chocolatey.org/packages/npm
Followed this advice and chocolatey installed an incredibly old version of npm which overrode my slightly-old version, causing very bad things to happen.
P
Peter Mortensen

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.

https://i.stack.imgur.com/PcfrZ.png


Sure it does @BrianDiPalma. Added screen shot to show upgrade from 2.14.12 to 3.8.2
n
naXa stands with Ukraine

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

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.cmd the new npm will be at C:\Users\username\appdata\roaming\npm\npm.cmd

Hope this helps.


This solution modifies the nodejs directory, leaving behind an orphaned npm. If you have more than 1 user on the machine, you just broke NPM for everybody else. This method also causes problems with global packages. the npm installed via this answer does not have the npmrc file, so global packages going forward will likely be installed in the wrong directory. If you update node, the nodejs/npm.cmd is now restored. So if nodejs updates npm, you are stuck on the old one unless you run that command again.
B
BrunoLM

Open PowerShell as administrator.

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

$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.exe and update npm with

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.

I went a little further and decided to implement a nvm for Windows.

https://github.com/brunolm/nvm

Install-Module -Name power-nvm

nvm install latest
nvm default latest

Excellent answer. Many would of course use C:\Program Files\nodejs\node.exe for the last part.
o
ofir_aghai

1. Installing latest npm version

npm install –g npm@latest 

(You can type "npm –version" to check that)

2. Installing Node

a. Install node new version via following URL: https://nodejs.org/en/download/current/ Follow the default choices
b. Remove C:\Users\\AppData\Roaming\NPM
c. Remove C:\Users\\AppData\Roaming\npm-cache

Optionally:

d. (Delete node_modules folder in your current project folder) e. npm cache verify f. npm install


This worked for me..You might need to use npm rebuild node-sass --force after you are done with installation because your environment has changed
P
Peter Mortensen

Use Upgrade npm on Windows

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

Here is my screenshot!

https://i.stack.imgur.com/snpdn.png


msi is also another choice!
From the page you linked to: "This is a small tool made by Microsoft DX engineers". So saying "this is the official document" is maybe a bit misleading. It sounds like you're saying it's official from the node or npm folks.
P
Peter Mortensen

For what it's worth, I had to combine several answers...

Uninstall Node.js in control panel Add/remove programs. Delete directories, both C:\Program Files (x86)\nodejs\ and C:\Program Files\nodejs\ if they exist. Install the latest version, http://nodejs.org/download/


Can you remember anything specific why you had to do this? I just tried it with downloading, installing and it worked instantly.
No, But i think that the older version of node installed it in a different windows directory in the path. At least thats my assumption
P
Peter Mortensen

How to Update Node.js:

Uninstall Node.js. Click the Start menu, type "Change or Remove a Program", click on the item shown, find Node.js in the list and uninstall it. Delete directories, both C:\Program Files (x86)\nodejs\ and C:\Program Files\nodejs\ if they exist. Install the latest, https://nodejs.org/en/download

The uninstall/delete/install seems unnecessary, but it often is and this will save your time. These instructions come from Microsoft.

How to Update NPM:

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

This is the official documentation for upgrading npm on windows.

All was tested and working on Windows 10 (2017).


A
Adiii

this is best tool to maintain version of NODE.Js i NVM

https://i.stack.imgur.com/k88UI.jpg


Most powerful and convenient answer here? UPVOTE this one, people! If you agree.
L
Lonnie Best

For me, after totally uninstalling node 10.29, and then installing node 4.2.2, there remained a 10.29 node.exe file in my c:\windows folder.

I found this by using the following command:

where.exe node

The command returned:

C:\Windows\node.exe
C:\Program Files\nodejs\node.exe

So even though I had successfully installed version 4.2.2 via the msi executable, the command node -v would continue to report I was running version 10.29.

I resolved the problem by deleting this file:

C:\Windows\node.exe

Thereafter, node -v reported the upgraded version instead of the unwanted remnants of the prior version.


m
manish kumar

For NodeJS

Download required node version msi from here and install

for Npm

Run PowerShell as Administrator

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

A
Abdullah Ibn Mannan

This works fine for me

Run Command Prompt as Administrator Navigate to the folder containing nodejs (eg. C:\Program Files\nodejs) Run Powershell -ExecutionPolicy Unrestricted Run npm-windows-upgrade This will show list of versions available to install. Just select your desired version by moving up/down key & Press Enter. This'll update your npm To check the current version of npm Run npm --version

Command Prompt Screenshot


c
cilerler

I was also facing similar issues. I followed below mentioned steps and it worked for me:

go to Windows > Start > Node.js right click on Node.js command prompt click on Run as administrator

right click on Node.js command prompt

click on Run as administrator

ping registry.npmjs.org

npm view npm version

cd %ProgramFiles%\nodejs

npm install npm@latest

and npm updated successfully. Earlier I was trying for CMD and that was throwing error. may be some path issue that got resolved by running NodeJs Command Prompt. hope it'll work for you. try this.


P
Peter Mortensen

OK guys, I read (tried on Windows) all the previous stuff and all of these answers have their own disadvantages.

For the best way to update Node.js (at least for me), go to https://nodejs.org/en/ Then download the last version and install it in same folder you installed the previous version in - 1 min and it's done. You don't need to remove any old files.

Then update npm typing in cmd: npm install --save latest-version


Maybe during trying other solutions i accidentally updated npm. To update it type in cmd: npm install --save latest-version
Sometimes I still asks myself WHY I use windows. Thanks for your simple solution.
P
Peter Mortensen

To install the updates, just download the installer from the Nodejs.org site and run it again. The new version of Node.js and NPM will replace the older versions.


this didn't work in my case with windows 10. I have npm v5.4.2 and node 7.5.0. When I wanted to install node v9.0.0 npm -v says 5.4.2 not 5.5.1 as it should be
P
Peter Mortensen

The easiest way I found so far to update Node.js is using Chocolatey. Use Chocolatey to install or update the latest version of Node.js on Windows:

Step 1: First, ensure that you already have Chocolatey installed. If not, use an administrative shell to install chocolatey through cmd.exe or PowerShell.exe. For more information, visit: https://chocolatey.org/docs/installation

Step 2: Install with cmd.exe. Run the following command:

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

To install with PowerShell.exe, visit: https://chocolatey.org/docs/installation

Step 3: Install or Update with following commands on cmd.exe (on administrative mode)

To Install Node.js: cinst nodejs.install

To Update Node.js: cup nodejs.install


H
Hafiz Shehbaz Ali

follow these steps for window 10 or window 8

press WIN + R and type cmd and enter

npm i -g npm@next

npm i -g npm@next OR npm i -g node@{version}

Remove environment path C:\Program Files\nodejs from envrionment variable PATH.

type refreshenv in cmd

Now you will have your new version which you installed.

Note: If you don't remove path. You will see the previous version of node.


P
Peter Mortensen

I followed josh3737 and installed the latest MSI from the Node.js homepage.

But I had the additional problem that I still had the old version of Node.js and npm on the command line. The problem was caused by the new installation, and that it was installed into

C:\Program Files (x86)\nodejs\

instead of the previous installation in

C:\Program Files\nodejs\

The new installation added the new directory into my path variable after the old one. So the old installation was still the active one in the path. After removing C:\Program Files\nodejs\ from system path and C:\Users\...\AppData\Roaming\npm from user path and restarting the command line the new installation was active.

Maybe the least path was a local problem that has nothing to do with the new installation. I had two links to AppData\Roaming\npm in it. And maybe this can also be fixed by first uninstalling Node.js and installing the new version afterwards.


P
Peter Mortensen

PowerShell does not execute npm directly, so I suggest using

.\npm install -g npm-windows-upgrade
.\npm-windows-upgrade

And it failed with:

You wanted to install npm 6.1.0, but the installed version is 3.10.10.

A common reason is an attempted "npm install npm" or "npm upgrade npm". As of today, the only solution is to completely uninstall and then reinstall Node.js. For a small tutorial, please see http://aka.ms/fix-npm-upgrade (dead link).

Please consider reporting your trouble to npm-windows-upgrade.


J
Jitendra Pawar

You can use these commands:

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

If you are upgrading from a previous version of node, then you will want to update all existing global packages. You can also specify the package name to be updated.


r
robinhood9

This might help someone. Neither "npm-windows-upgrade" nor the installer alone did it for me. Powershell was still using an older version of node and npm.

So this is what I did (worked for me): 1. Download the latest installer from nodejs.org. Install node. It will update your node; everywhere (Powershell, cmd etc.). 2. Install the npm-windows-upgrade package (npm install -g npm-windows-upgrade) and run npm-windows-upgrade.

I didn't uninstall anything and didn't set any paths.


"Powershell was still using an older version of node and npm." You likely did some other update method in the past by modifying the path or running npm i -g npm. If you run into any issues in the future then you must do this: github.com/felixrieseberg/…
P
Peter Mortensen

In my case, I discovered that I had two copies of Node.js installed. One under "C:\Program Files\nodejs" and another under "C:\Program Files (x86)\nodejs".


N
Nithin Baby

This is what worked for me.

Open a local folder other than the one in which nodejs is installed. Install npm in that folder with command npm install npm Navigate to the folder containing node js. (C:\Program Files\nodejs\node_modules) Delete the npm folder and replace it with the npm and bin folders in the local folder. Run npm -v. Now you would get updated version for npm.

Note: I tried installing npm directly in "C:\Program Files\nodejs\node_modules" but it created errors.


K
Kshri

Start Search for windows powershell Right click and run as administrator Type: where.exe node (returns the path of node.exe in your system. Copy this) wget https://nodejs.org/download/release/latest/win-x64/node.exe -OutFile 'PATH-OF-NODE.EXE_WHICH_YOU_COPIED_JUST_NOW' To check if it has worked, go to your Git bash/Normal command prompt and type: node -v Here you can find the current version of node: https://nodejs.org/en/blog/release/