Toggle between node versions with nvm
If vim makes you cringe, I got you. In this guide we will also run through basic vim commands
You're in a project where your node version doesn't meet the dependency requirement. F your life, right? In the past I would have to uninstall node, and reinstall whatever node version I needed. Unfortunately my 9-5 has too many projects with conflicting version requirements that uninstalling and reinstalling just wan't a good use of time.
Nvm is a node version manager. You can instantly change your node version from the command line. ๐ช๐ฝ If you don't need your hand held, here are the docs.
Prerequisites
Lets make sure you're set up for success
- Mac user (sorry pc's ๐ฅฒ )
- You have โ๏ธ homebrew installedย
If you're unsure if you have homebrew, run
brew -v
. If the command is not found, you probably don't have it installed.
brew uninstall --ignore-dependencies node
brew uninstall --force node
๐ก If you get the following error
Error: No such keg: /usr/local/Cellar/node
Keep going.
Install nvm
Update brew and install nvm
brew update
brew install nvm
Create nvm directory
mkdir ~/.nvm
Create a new hidden file called bash_profile
touch ~/.bash_profile
Vim Basics
If you know how to exit a vim window, move on to the next section.
Create and open a practice file
If you have VSCode,
code
will open your file using VSCode. If you don't have VSCode installedcode
will not work โ๏ธ
touch practice.html
code practice.html
Add and save some text to the file.
Go back to your terminal
vim practice.html
Vim has opened your file in a readonly status. To edit the file, hit i
to change into insert mode. Make your edits. To leave insert mode back to readonly hit esc
. To exit vim, hit :wq {enter}
- insert:
i
- exit insert/save:
esc
- exit vim:
:wq {enter}
โจ more commands (ty vim girl)
Nvm
Setup
Open vim
vim ~/.bash_profile
Copy and paste the following text in the file. Save and close.
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Activating
If you try to run nvm --version
you will get an error that the command is not found. That's because nvm hasn't been activated.
Activate nvm
source ~/.nvm/nvm.sh
Check that nvm was activated by running nvm --version
again.
๐ก Next, we're going to create an alias for that command .
Creating an alias
In terminal
vim ~/.bash_profile
Copy and paste the following text and save it to .bash_profile
alias start_nvm="source ~/.bash_profile"
You can use a different variable name if you don't like
start_nvm
Restart terminal
๐บ๐บ๐บ If you are using iterm2, keep reading ๐บ๐บ๐บ
If you use iterm2 as your default terminal then you need to update .zschrc
.
vim ~/.zshrc
Copy and paste the following text and save it to .bash_profile
alias start_nvm="source ~/.bash_profile"
Restart terminal
Installing node
To view a list of all the available node versions run nvm ls-remote
. It's a long list, be patient ๐ง๐ฝโโ๏ธ.
Installing node versions
nvm install 14
nvm install 14
nvm install 15
To view the list of versions you have installed, run nvm ls
Sets node version 18 as default
nvm alias default 18
node -v
Swaps for a different version
nvm use ${nodeV}
node -v