How to update npm

For today’s post, we will cover an issue that may come up from time to time particularly when you have to support some ancient application that was built on an older version of node.js or more specifically, uses an older version of npm. This will be a walk through on how to update the older versions of npm on Windows.

What is npm?

The error I ran into was one specifically related to updating npm, which is the package manager that comes bundled with the Node JavaScript platform. You can configure npm to use any compatible registry you like but it comes preconfigured to use npm’s public registry by default.  Here is a link to the npm docs if you are interested in learning more:

https://docs.npmjs.com/

Now back to the problem at hand. I needed to use an older version of node.js with a newer version of npm. My original thought was that I could use npm to update itself. It is a package manager after all and npm is a package.

npm updating npm
Using npm to update… npm

How to update npm via CLI

This is the preferred method of updating npm. We are essentially telling npm to go ahead and update itself. You will want to open a PowerShell or Bash prompt and type the following command.

npm install -g npm@latest

Note that the -g makes it globally available and the @latest will pull the most recent version however you can use this to target a specific version as well. An example would be running:

npm install -g [email protected]

Unfortunately after running the command above, I was greeted with the error below. Notice the version of npm I am using.

initial error with npm install

For some of the older versions of npm, this approach is a bit problematic. I found that it does work as intended with the newer versions of npm so this issue seems to only affect the older versions. Using Node.js version 15 and above will allow you to update npm using the commands above since they ship with v7+ versions of npm.

There are several solutions we can take to solve this issue. One suggestion often thrown out is ditching NPM all together and using Yarn, pnpm, Chocolatey or some other package manager.

But what if you are in a situation where you can’t use these tools and must resort to using the default version of npm you are given? My thoughts are also that you really shouldn’t need to use another package manager to manage your current package manager.

Now there may be an easier or automated approach to solving this issue but for the time its worth, here is what I do/did/ and will probably do next time this happens.

If you are following along with my blog, then you should have all your tools conveniently located in one place.

location of npm tools at C:\tools
C:\tools

To get a recap on what/how/why we did this, check out this post:

How to manually update npm

Click on the nodejs symlink

nodejs symlink directory
C:\tools\nodejs

The first thing we are going to do is rename npm.cmd to npm1.cmd (we will use this file later)

rename npm.cmd to npm1.cmd
Rename npm.cmd -> npm1.cmd

Next we need to delete a few files: npm, npx, npx.cmd (highlighted below)

delete additional files: npm, npx, npm.cmd
Delete these three files: npm, npx, npx.cmd

Your nodejs symlink directory will now look like this

symlink directory updated

Then we are going to run this command (note the npm1 as reference to the file we renamed earlier)

npm1 install -g npm@latest

If it works then great! Problem solved! This was all I had to do the first time I went through this however when I went through the steps for this blog, I encountered a few additional errors which we will address below.

Additional errors

If you get an error like this, you can just delete the .bin folder listed in the error below. Don’t worry, these are files that will be all overwritten once we upgrade npm anyway.

Refusing to delete error 1

Browse to C:\tools\nodejs\node_modules\npm\node_modules and delete the .bin folder

.bin location in node_modules
Delete C:\tools\nodejs\node_modules\npm\node_modules\.bin

I also received another similar error right after fixing the last one, so we get to delete another .bin folder but this time under the node-gyp directory.

Refusing to delete error 2

Browse to C:\tools\nodejs\node_modules\npm\node_modules\node-gyp\node_modules and delete the .bin folder

.bin folder location in node_modules
Delete C:\tools\nodejs\node_modules\npm\node_modules\node-gyp\node_modules\.bin

Now we the npm1 install command from before. (You can just push the up arrow and it will save you from having to write out the command again).

Successful completion of command
Success! Now go celebrate!

A bit of cleanup

You can delete the old npm1 file we used to update npm as it is no longer needed.

Clean up and remove old npm1.cmd file
Delete npm1.cmd

Cheers!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.