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.
So if you have a Mac M1, these are the steps I would encourage you to try.
Before proceeding to the next step I needed to manually install Rosetta 2 in order run apps not built for Apple silicon.
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
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.
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!
nvm use 16
nvm use --lts
Finally, to clear the nvm cache to reduce clutter, run:
nvm cache clear
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
nvm install 10.19.0
nvm alias default 10.19.0
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.
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 and Michael Baldwin who both have good information on this topic, so much thanks to them where all credit is due...