NVM stands for Node Version Manager which is a command-line tool used to install and manage multiple Node.JS versions. You can have multiple installations of Node.JS in a single account and decide which version to use for your project. The installation of Node.JS is user account specific and other user accounts can have their own independent installations.
There are different ways of installing Node.JS. One of them is to use the APT package manager which installs it from Debian repositories. However, this does not always install the latest version. Another way of installing NodeJS is using the NVM utility.
In this context, we shall look into the process of installing NVM on Debian 11.
Different ways to install NVM on Debian
1. Download and install the NVM utility
To install NVM, you need to, first, download and run the NVM bash script as follows:
$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
The command downloads nvm as a script to the .nvm folder in the home directory. It then appends the environment variables for NVM in the .bashrc file.
Next, reload the .bashrc file to apply the changes:
$ source ~/.bashrc
Alternatively, simply log out and log in again and confirm the version of NVM installed:
$ nvm --version
2. Install and Manage NodeJS using NVM
As mentioned earlier, NVM allows developers to install multiple versions of Node.JS. To install the most recent version of NodeJS run the following command with 'node' as the alias for the latest version:
$ nvm install node
If you want to install the latest LTS (Long Term Service ) version, run the command:
$ nvm install node --lts
In our case, the latest version – v16.10.0 – is already installed.
You can also specify a specific version of NodeJS by specifying the version number. Here, we are installing Node.JS v14.18.0:
$ nvm install 14.18.0
To list all the versions of Node.JS installed, execute:
$ nvm ls
To switch to a particular Node.JS version run the command as shown. Here, we are switching to Node v16.10.0:
$ nvm use 16.10.0
To verify the default version already set in your account, run:
$ nvm run default --version
You can list all the available Node.JS versions which are available for download with the below command:
$ nvm ls-remote
Finally, you can execute a Node.JS script with your preferred version with the below command:
$ nvm exec 14.18.0 backoffice.js