Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 16, 2021 09:01 am GMT

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. Lets say ones 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 nvmcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

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

Check if nvm installed correctly by running:

nvm -v# prints nvm help menu for various options

terminal screenshot showing nvm

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.0node -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


Original Link: https://dev.to/nmurgor/how-to-easily-switch-between-different-versions-of-nodejs-on-your-system-7e8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To