Installing a New Node Version and Migrating npm Global Packages
Tags: node, npm, nvm, beginners
Published: 2019-08-16 19:56:35

So I recently had to install a new Node version because we (dev.to) upgraded it. Or at least I think that's the reason -- I just do what the command line tells me to do.

I knew that I was already using [`nvm`](https://github.com/nvm-sh/nvm) or Node Version Manager. It works pretty great and similar to [`rbenv`](https://github.com/rbenv/rbenv), the Ruby version manager I use. Looking at `nvm`'s readme, all I had to do was:
1. figure out the version of Node I needed
2. run the install command

So, I looked at Node's available versions on their [home page](https://nodejs.org), and I went for the latest LTS (long term support) version -- 10.16.0. I probably could've went for the latest at the time, 12.8.1, but since I was running version 8.11.3 I thought it was safer to go up only two major version upgrades. It probably doesn't matter _that much_ though.

Then I ran the install command:
```bash
nvm install 10.16.0

# I would put the output but I installed this like 3 weeks ago :(
```

...it completed, great! Nothing seemed to go wrong, and there didn't seem to be anything else to do.

I ran `yarn` on my local dev.to repo, but it failed:
```bash
:yarn
yarn install v1.16.0

# [1/4] 🔍  Resolving packages...
# [2/4] 🚚  Fetching packages...
# error execa@2.0.3: The engine "node" is incompatible with this module.
# Expected version "^8.12.0 || >=9.7.0". Got "8.11.3"
# error Found incompatible module.
# Visit https://yarnpkg.com/en/docs/cli/install
# for documentation about this command.
```

Hmm guess I was still on my old version. Reading `nvm`'s readme again, I had to actually start using the new Node version I installed:
```bash
nvm use 10.16.0
# Now using node v10.16.0 (npm v6.9.0)
```

Okay great! That seems to work. I ran `yarn`, and it worked successfully!

Fast forward to some weekend later, I was working on a Gatsby side project, and I try to start the development server:
```bash
gatsby dev
#-bash: gatsby: command not found
```

Hmm, well I figured this would probably have to do with my new Node version. I found a command to reinstall the packages from a previous version:
```bash
nvm reinstall-packages 8.11.3
# Reinstalling global packages from v8.11.3...
# No installed global packages found...
# Linking global packages from v8.11.3...
# No linked global packages found...
```

Strange, not sure why that happened. Let's take a look at what versions I have installed:
```bash
nvm list
#         v8.11.3
# ->     v10.16.0
#          system
# default -> node (-> v10.16.0)
# node -> stable (-> v10.16.0) (default)
# stable -> 10.16 (-> v10.16.0) (default)
# iojs -> N/A (default)
# lts/* -> lts/dubnium (-> v10.16.0)
# lts/argon -> v4.9.1 (-> N/A)
# lts/boron -> v6.17.1 (-> N/A)
# lts/carbon -> v8.16.0 (-> N/A)
# lts/dubnium -> v10.16.0
```

Oh hm, I wonder if my packages were installed from the system version instead of 8.11.3. I ran the reinstall command again, except for system:
```bash
nvm reinstall-packages system
# some successful output
```

Great! Let's try `gatsby develop` again. Nope, didn't work. Still getting `command not found`. Well, I have two options: either use the version with my global packages installed every time I need to use a global package, or figure out the issue.

Actually, there's a third option: do the first option until I get sick and tired of it over for four weeks and then finally figure out what's wrong. As a generally lazy person, I went for that and here I am writing this post.

Ahem ANYWAY, I had a general sense that it was because some `npm` and `nvm` linkage was not correct. This was confirmed (probably) by the fact that running `gatsby develop` would fail and return `command not found`, as well as:
```bash
which npm
# /Users/andyzhao/.nvm/versions/node/v8.11.3/bin/npm
```

Well, when I first tried to fix it, I sunk many hours searching, but to no avail. This time, I found an answer from Stack Overflow that helped me:

{% stackoverflow 23586069 %}

Specifically, the command `npm config ls -l` gave me some clues that I needed:
```bash
npm config ls -l
# ; cli configs
# long = true
# metrics-registry = "https://registry.npmjs.org/"
# scope = ""
# user-agent = "npm/6.9.0 node/v10.16.0 darwin x64"
# 
# ; userconfig /Users/andyzhao/.npmrc
# prefix = "/Users/andyzhao/.nvm/versions/node/v8.11.3"
# ...whole lots of other stuff
# globalconfig = "/Users/andyzhao/.nvm/versions/node/v8.11.3/etc/npmrc"
# ...etc.
```

So my `globalconfig` was set to Node v8.11.3, and not my current version v10.16.0. Seeing that my prefix was also 8.11.3, I decided to open up my `userconfig` file `.npmrc` and find out if there was something I could change from there.

```bash
# .npmrc

prefix=/Users/andyzhao/.nvm/versions/node/v8.11.3
```

Ah ha! That must be it! Right? I dunno, but worth trying! I updated it to v10.16.0, restarted Terminal, and ran `nvm reinstall-packages system`. `npm` proceeded to run and install everything, and I saw that `gatsby-cli` was installed. Restarted Terminal again to be sure, and ran `gatsby develop`. Success!!!

Phew, what a trip! Figuring this out definitely took longer than I wanted, but I'm glad I managed to get it. While I definitely did just switch versions over and over again for 3 weeks before doing anything, I think it's always helpful to take a break from a problem and come back with fresh eyes. Here's hoping no one else has to figure out `nvm` and `npm` installation issues. ✌️