loading...
Cover image for Installing a New Node Version and Migrating npm Global Packages

Installing a New Node Version and Migrating npm Global Packages

andy profile image Andy Zhao (he/him) ・4 min read

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 or Node Version Manager. It works pretty great and similar to 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, 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:

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:

: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:

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:

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:

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:

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:

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:

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:

trying to install global packages into C:\Program Files (x86)\nodejs\ gave me Run as Administrator issues, because npm was trying to install into
C:\Program Files (x86)\nodejs\node_modules\

to resolve this, change global install directory to C:\Users\{username}\AppData\Roaming\npm:

in C:\Users\{username}\, create .npmrc file with contents:

prefix = "C:\\Users\\{username}\\AppData\\Roaming\\npm"

reference

  • npm install -g…

Specifically, the command npm config ls -l gave me some clues that I needed:

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.

# .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. ✌️

Discussion

markdown guide
 

You can try npm root -g to see where the global node_modules folder is.

 

That's the type of thing that makes documentation so important! Little details that were lacking made you lose your time figuring out issues that the tool should have covered. Thanks for sharing!

 

Just install this and forget about rbenv, nvm and others