Setup Node Version Manager (NVM) on macOS M1 Silicon
Tags: node, javascript, devops
Published: 2021-11-14 09:50:41

As a web developer these days, we typically need to be able to switch versions of Node on the fly, for this we want to install Node Version Manager on a clean install of our machine, we don't want to start by installing Node on its own as this will give us a single version of Node (whichever we decide to install)

If you install Node first and then try to install NVM, things can get complicated, so if you have already installed Node, my suggestion is to completely remove it before installing NVM.

As well, NVM is explicitly not supported when installed via homebrew - the only correct way to install it is with the install script in [NVM's Readme](https://github.com/nvm-sh/nvm/blob/master/README.md#install--update-script).

So if you have a Mac M1, these are the steps I would encourage you to try.

## Navigate to your home directory

    cd ~

## Create a `.zshrc` file (if it doesn't exist)

    touch .zshrc

Before proceeding to the next step I needed to manually install Rosetta 2 in order run apps not built for Apple silicon.

    softwareupdate --install-rosetta

## Install NVM using curl (found on the NVM Readme)

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

This last command will update your `.zshrc` file to look like the following:

    export NVM_DIR="$HOME/.nvm"
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
    [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

## Install Node using NVM

    nvm install node

This will install the latest version of Node (v`17.x` at time of writing).

NOTE: Node versions before v`15.x` are not necessarily ARM compatible, but it seems that Node has addressed this issue, so if you do install a version prior to v`15.x` hopefully, you will not need to use Rosetta to run.

## Install LTS version of Node

    nvm install --lts

Running this command installed the current Node LTS which at the time of this writing is v`16.x`. I think I will try to stick with this version or better when developing, however; that's the beauty of NVM is that if I need and older version it's easy to switch!

## List the versions of Node I have installed

    nvm ls

## Select an alternate version that I have installed

    nvm use 16

or 

    nvm use --lts

Finally, to clear the nvm cache to reduce clutter, run:

    nvm cache clear

## Setting Default

    nvm alias default v10.19.0

But it will give the following error

    ! WARNING: Version 'v10.19.0' does not exist. default -> 
    v10.19.0 (-> N/A)

In that case, you need to run two commands in the following order

### Install the version that you would like

    nvm install 10.19.0

### Set 10.19.0 (or another version) as the default

    nvm alias default 10.19.0

## Use a Specific Node Version

    nvm use 12
    Now using node v12.22.3 (npm v6.14.13)

Now we are using that latest version of node. As time goes on, I could keep periodically running:

    nvm install 12

And get any updates to the latest of that version, or set a more specific earlier version, and then I would have two different versions of 12 I could go back and forth if needed during development.

## Uninstall a Node Version

    nvm uninstall 12
    nvm: Cannot uninstall currently-active node version, 
    v12.22.3 (inferred from 12).

Notice the comment about it inferring because we were not specific. But, we are on the node we are trying to delete, we need to get off of this version to delete it:

    nvm use 16 && nvm uninstall 12
    Now using node v16.4.2 (npm v7.18.1)
    Uninstalled node v12.22.3

This is not a topic widely talked about in one easy to find resource and the best sources I have found on the subject came from [Code Fallacy on YouTube](https://www.youtube.com/watch?v=LOd0Dx_ZrYQ) and [Michael Baldwin](https://twitter.com/mkbaldwin21) who both have good information on this topic, so much thanks to them where all credit is due...