DEV Community

Cover image for How to easily switch between different versions of Nodejs on your system.
Naftali Murgor
Naftali Murgor

Posted on

How to easily switch between different versions of Nodejs on your system.

Introduction

Easily switch between different versions of Nodejs on your system.

In this blog article we'll learn how to switch to a default version when using nvm

NVM is a tool that handles what versions of Nodejs you can use. Let’s say one’s working on a cutting edge library that requires the latest version, they would switch/install a version of Nodejs that is compatible with the library.

Scenario two, one is working on a project that requires an older version of Nodejs, let's say version 8.0.0. Installing and reinstalling Nodejs becomes hectic and cumbersome.

nvm makes handling versions of Nodejs rather painless.

NVM, (Node Version Manager) enables one to:

  1. Install different versions of Nodejs
  2. Switch to different versions of Nodejs
  3. Set a default Nodejs version from the installed versions
  4. Remove installed versions of Nodejs

Install nvm

This assumes that nvm installed already, if not, install nvm by:

# install script for nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

Note: Curl installation on your system is also required. Curl enables one to make http request from the commandline.

After downloading and running the bash script, set your profile file ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc so that nvm is available system-wide.

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Enter fullscreen mode Exit fullscreen mode

Check if nvm installed correctly by running:

nvm -v
# prints nvm help menu for various options
Enter fullscreen mode Exit fullscreen mode

terminal screenshot showing nvm

Install a different version of nodejs

To install a different node version using nvm:

nvm install 14.0.0
Enter fullscreen mode Exit fullscreen mode

NVM handles the installation of the Nodejs version for you, afterwards , you may use this version when needed or as needed:

To use the Nodejs version from above:

nvm use 14.0.0
Enter fullscreen mode Exit fullscreen mode

This command tells NVM to switch Nodejs to this version, the changes apply system-wide which is kinda cool, isn't it?

Set a default version of Nodejs using NVM

To set a default version of Nodejs using nvm, use this syntax:

nvm alias defaut <your_nodejs_default_version>
Enter fullscreen mode Exit fullscreen mode

To switch to version we installed above 14.0.0, run:

nvm alias default 14.0.0
node -v # prints 14.0.0
Enter fullscreen mode Exit fullscreen mode

NVM makes handling nodejs versions on your system rather painless and easy especially if you heavily use Nodejs as a tooling for your frontend work flow.

NVM offers more options such as:

  • uninstall a Nodejs version
  • Switch to a Nodejs version, nvm use <nodejs_version>

Further refference:
https://github.com/nvm-sh/nvm

Discussion (5)

Collapse
itsmurgor profile image
Murgor πŸ‡°πŸ‡ͺ

To check if a new node version is available:

nvm ls-remote

Collapse
lukeshiru profile image
Luke Shiru • Edited on

Try n. Is like nvm but faster.

Collapse
ivanjeremic profile image
Ivan Jeremic

pnpm does it too

Collapse
bmaniar profile image
Bhargav Maniar

I prefer volta. It's automatic changing feature is great and you can use it for any global packages.

Volta : github.com/volta-cli/volta

Collapse
naftalimurgor profile image
Naftali Murgor Author

Great, Will try that ou!