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:
- Install different versions of Nodejs
- Switch to different versions of Nodejs
- Set a default Nodejs version from the installed versions
- 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
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
Check if nvm
installed correctly by running:
nvm -v
# prints nvm help menu for various options
Install a different version of nodejs
To install a different node version using nvm:
nvm install 14.0.0
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
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>
To switch to version we installed above 14.0.0
, run:
nvm alias default 14.0.0
node -v # prints 14.0.0
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