w3resource

How to Upgrade to the Latest Stable Version of npm


In the last tutorial we recommended that you always use a stable version of Node we also gave you instructions on how to install a stable version of Node for different operating system.

In this tutorial we are going to show you how to check you npm version, as well as how to upgrade node for different operating systems.

To see the version of npm that you are running, you will need to run the following command:

npm -v

Upgrading on *nix (OSX, Linux, etc.)

To upgrade your npm version, you can either upgrade the latest version of npm using:

npm install -g npm@latest

or you can upgrade to the most recent release:

npm install -g npm@next

In most cases, you may have to prefix these commands with sudo, especially of Linux, or OSX if Node was installed using its default installer.

Upgrading on Windows

Npm is installed alongside node inC:\Program Files (x86 )\nodejs by default. While its globally installed packages are stored separately in a user-specific directory(C:\Users\<username>\AppData\Roaming\npm).

This is because the installer will put C:\Program Files (x86)\nodejs

Before

C:\Users\<username>\AppData\Roaming\npm

On your path, it always uses the version of npm that is installed with node, rather than the version of pm that you installed using npm ?g install npm@<version>

For you to get around this, you have the following options available to you:

  • Option 1:you can edit your Windows installation's PATH to put %appdata%\npm before %ProgramFiles%\nodejs. You should remember that you will need to restart cmd (and potentially restart Windows) whenever you make changes to PATH or how npm is installed.
  • Option 2: remove both
    • %ProgramFiles%\nodejs\npm and
    • %ProgramFiles%\nodejs\npm.cmd
  • Option 3: You can navigate to %ProgramFiles%\nodejs\node_modules\npm and copy the npmrc file to another folder or the desktop. Then open the cmd and run these commands:

cd %ProgramFiles%\nodejs

npm install npm@latest

if npm is installed with the nodejs installer, after following any of the previous steps, you can do the following:

  • Option 1 or 2
    • You need to go into %ProgramFiles%\nodejs\node_modules\npm and copy the file that is named npmrc in the new npm folder, this should be%appdata%\npm\node_modules\npm. This tells the new npm where the global installed packages are.
  • Option 3
    • You should copy the npmrc file back into %ProgramFiles%\nodejs\node_modules\npm

The steps above have a small command line tool to automate it. You can download it here https://github.com/felixrieseberg/npm-windows-upgrade

Note on the built-in windows configuration

The node installer will install directly into the npm folder, a special piece of windows-specific configuration that tell npm where it should install global packages. Whenever npm is used to install itself, it is supposed to copy this builtin configuration into the new install. However, there was a bug in some versions of npm that prevented this from working, so may have to fix that manually. To find out if your npm install location is correct, you will need to run this command:

npm config get prefix ?g

if it is not set to <X>:\Users\<user>\AppData\Roaming\npm, you will have to run the command below to correct it:

npm config set prefix "${APPDATA}/npm" ?g

However, if you do not want packages to be installed to your roaming profile, you can put it inside your local app data instead:

npm config set prefix "${LOCALAPPDATA}/npm" ?g

as well copying the %APPDATA%\npm to %LOCALAPPDATA%\npm (and then you update your %PATH%).

Your npm is broken

If your npm is broken, you have to reinstall npm using Node?s official installer.

Previous: Troubleshooting.
Next: Common npm Errors and How to Fix Them.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/npm/try-the-latest-stable-version-of-npm.php