How to Easily Manage Multiple Node Versions

Posted by

This will be a short tutorial that will teach you how to manage node versions using a version manager called nvm. You’ll also see how to install/uninstall different node versions and switch between them. 

Node has a lot of different versions. If you are working with multiple projects, there is a good chance you will need a tool to switch between node versions easily. One such tool is nvm

Note this tutorial is for Linux/Mac. If you are using Windows, you should check out this repository.

Installing NVM

Install NVM using one of the below commands depending on whether you use curl or wget

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh|bash
view raw gistfile1.txt hosted with ❤ by GitHub

or 

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh|bash
view raw gistfile1.txt hosted with ❤ by GitHub

After installation try running the following command

nvm --version

If you see an output, it means nvm has been successfully installed. If you get an error that the command can not be found, you will need to add it to your zsh/bash profile manually. 

Open your .zshrc.bashrc file

nano ~/.zshrc

Paste the following at the end

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
view raw .zshrc hosted with ❤ by GitHub

Restart your zsh / bash terminal

source ~/.zshrc

Installing a version of Node

nvm install <version_number>

If I wanted to install Node 14, I’d run the following command

nvm install 14

You can also install a specific patch version.

List All available versions of Node

nvm ls-remote

Switch Between Versions of Node

nvm use <version_number>

If I wanted to switch to Node 14, I’d run the following

nvm use 14

Aliases

Check the current version set to an alias

nvm alias <alias_name>

For example, if you want to check your current default version

nvm alias default

Another useful alias is node. It is an alias for the current stable version installed on your machine.

If you want to set your default to Node 14, run the following

nvm alias default 14

List the Current Version of Node

nvm current

Uninstall a Version of Node

nvm uninstall <version_number>

Run a Script using a Specific Version of Node

nvm run <version_number> <script.js>

Leave a Reply

Your email address will not be published. Required fields are marked *