If you are a developer focusing on web applications, chances are you have heard of Node.js and have used it daily. So, what exactly is Node.js and what is it useful for? Let’s see.
What is Node.js?
Node.js is a runtime environment for JavaScript. It can be used on Linux, MacOS, and Windows. You can use it on Linux, MacOS, and Windows. It is also open-source. It is used by developers to create web applications and server-side programming outside of the web engine using JavaScript code. It is beneficial for developers as they do not have to learn another language.
It is useful for both backend and frontend frameworks. It mainly allows the javascript to run independently, outside the browser. It runs on the V8 JavaScript engine.
Your project may contain codes from different versions of Node.js. Being able to view all of them is not possible if you use npm. npm only installs one version of Node.js, so your code will only be viewed in that one version.
What if you want to view a project that uses Node version 15.0.0 but you use version 12.0.0? Or vice versa?
This will bring up errors like “This project requires Node version X”.
To mitigate this and to bring about backward and forward compatibility, you need to do something else entirely. You need to be able to use, view, and switch between various Node versions.
Now, how do you install different Node versions and switch between them?
Here is where NVM comes in.
What is NVM?
Node Version Manager, or NVM, is a way for you to install and switch between different Node versions. It is used on Linux and MacOS devices to install Node.js on their devices easily. It can also be done on Windows, but that is a separate topic altogether.
In this article, we are mainly focusing on installing NVM and Node.js on devices with Ubuntu 22.04.
A word of caution: Remove any versions of Node.js you have already installed on your device before installing NVM. This will avoid any conflict between the previously installed Node versions and the new Node versions.
Installing NVM on Ubuntu
Installing NVM on Ubuntu is very simple. Here we will show you how to install NVM on Ubuntu 22.04 LTS and upward. You will need your Ubuntu to have shell access.
Log in to the user account where NVM and Node.js are to be installed.
Open a terminal on your system by pressing Ctrl+Alt+T.
Now enter these commands to first install curl on your system. We will then use this to run the script for the NVM installer.
sudo apt install curl
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
To load the environment, you can execute this command. Or just log out and log back in.
source ~/.profile
Now NVM is installed on your account. To complete the process, you will have to install the various versions of Node.js. The first Node you install will be set as the default. You can enter the specific version of the Node you want to install.
If you want the latest Node, enter this:
nvm install node
Or, you can also enter the version you want to install, like this:
nvm install 17.9.1
Other Commands For NVM
If you want to change your default Node version for the specific session, enter this:
nvm use 17.9.1
or any other version you may wish to use.
To view all the installed Node versions, run this command:
nvm ls
If you want to view what versions of Node can be installed, enter this:
nvm ls-remote
If you wish to run a Node script with a version that you desire, you can run this prompt:
nvm exec 16.14.0 server.js
If you want to remove any unwanted Node, enter this:
nvm uninstall 17.0.1
Installing NVM on Windows
NVM on Windows is easier than on Linux and MacOS. All you need to do is download and install the NVM installer setup file. The process is as follows:
You have to visit https://github.com/coreybutler/nvm-windows and download the NVM installer by clicking on the README file. In it, you will see the “download now” button. Click on it.
Download the latest nvm-setup.exe file.
Run the setup and install nvm for Windows.
After installation, run this command to see the version installed.
nvm -v
If the correct version is displayed, then the installation is successful.
Conclusion
Installing NVM on Linux and MacOS is very similar since both are UNIX-based operating systems. NVM for Windows supports Node.js, but not io.js.
NVM is a pretty useful tool for developers who use various versions of Node and constantly need to switch between them. It helps you install whichever version you desire and makes it easy to uninstall any unwanted Node.
Hope you found this article informative on your development journey.