Skip to content

Instantly share code, notes, and snippets.

@pagreczner
Last active March 15, 2021 14:20
Show Gist options
  • Save pagreczner/9c936fb0fbaa8aa42bb2 to your computer and use it in GitHub Desktop.
Save pagreczner/9c936fb0fbaa8aa42bb2 to your computer and use it in GitHub Desktop.
Downgrade NPM Version to a Lower / Earlier Version

Downgrade NPM version to an earlier version

Problem

Found myself at a later version of NPM that I didn't want, and had trouble finding the resources online to explain how to go to a lower version.

Was at 1.2, did npm update npm -g and found myself at 2.0 and then was not able to go down to the desired 1.4.

If you find yourself at a more current version than you wish, follow these steps:

Solution

  1. Wipe out your current version of npm

>sudo npm uninstall npm -g

  1. Download the install.sh file from the npm registry: [http://npmjs.org/install.sh]

  2. Open install.sh in a text editor such as Sublime and find the following code:

t="${npm_install}"
if [ -z "$t" ]; then
  # switch based on node version.
  # note that we can only use strict sh-compatible patterns here.
  case $node_version in
    0.[012345].* | v0.[012345].*)
      echo "You are using an outdated and unsupported version of" >&2
      echo "node ($node_version).  Please update node and try again." >&2
      exit 99
      ;;
    v0.[678].* | 0.[678].*)
      echo "install npm@1.1"
      t=1.1
      ;;
    *)
      echo "install npm@latest"
      t="latest"
      ;;
  esac
fi

Determine which branch belongs to you. For example, if you are using node version 0.8, then you'd want to change echo "install npm@1.1" to the proper version number of npm that you want, such as

echo "install npm@1.4"
t=1.4
  1. Run the install.sh script: sh install.sh

You should now have the desired version of npm install on your system. Remember to install any modules you may have deleted in this process.

@EMPAT94
Copy link

EMPAT94 commented Mar 15, 2021

My 2 cents for whoever ends up here

Context :
I got on npm v7 for some reason, but was using node 8 thus giving incompatible warning. Wanted to revert to npm v6.

Problem:
$ npm install -g npm@6 succeeded without errors but $ npm -v still showed v7.

Solution:
While I have no doubt this gist would work, I tried another (easier) method - basically removing the symlink of npm v7.

$ which npm showed /usr/local/bin/npm --> /usr/local/lib/node_modules/npm

I rmed above node_modules and doing $ npm -v showed v6 as it should!

Note:
I have an npmrc with prefix=~/.npm-global, where my npm v6 (and other global modules) are installed (as opposed to the default dir).
I'd suggest removing the symlink first and see if works before rm just to be safe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment