Node.js and npm With NVM

Chanudi Tharushika
MS Club of SLIIT
Published in
4 min readMay 17, 2023

A well-liked JavaScript runtime called Node.js enables you to create scalable and fast applications. The default package manager for Node.js is npm (Node Package Manager), which gives users access to a large ecosystem of open-source tools and libraries. NVM (Node Version Manager) is a useful tool for managing multiple Node.js versions and switching between them effortlessly.

What is NVM ?

A command-line tool called NVM (Node Version Manager) allows you to install and manage various Node.js versions on your computer. It gives a simple method to change between various Node.js versions, ensuring compatibility with your projects.

Why Use NVM ?

Multiple Versions:

NVM enables the simultaneous installation and use of multiple Node.js versions. When working on projects which make use of particular Node.js versions or when testing compatibility between different versions, this flexibility is important.

Easy Version Switching:

Using NVM, switching between different Node.js versions is simple. For a particular project, you can switch to a different version, or you can globally set a default version for all of your projects.

Isolated Environments:

For each version of Node.js that is installed, NVM creates isolated environments. To avoid conflicts between projects, each version’s dependencies, global packages, and configurations are specific to each version.

Easy Upgrades:

The Node.js upgrade process is made simpler by NVM. Without manually uninstalling and reinstalling Node.js, you can quickly update to the most latest stable version or switch to a specific version.

Compatibility Testing:

NVM enables you to test your projects on various Node.js versions to make sure they perform as expected in a variety of settings. This helps in the early detection and resolution of version-specific compatibility problems.

Installations

1. To download and install NVM, open your terminal or command prompt and enter the following command:

This command will download and run the NVM installation script on your computer.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

2. Close and reopen your terminal or command prompt after the installation.

To make sure the NVM command is recognized, it is important to complete this step.

3. Make sure NVM was installed correctly

nvm --version

4. You can now install Node.js after installing NVM. The latest stable version can be installed by using the command line as follow

nvm install node

NVM will download and install the most latest Node.js version.

5. Check of the installation

Run the following commands to verify that Node.js is installed correctly.

node -v or node --version

To ensure that npm is installed and updated correctly, run the following commands.

npm -v or npm --version

6. Install the latest Node.js and npm — LTS release

Let’s use nvm to install and use the most recent LTS version of Node.js now that it has been installed.

nvm install --lts

or

To update npm, run the following command:

npm install -g npm

Then you can verify the installation and working by running the command node -v and npm -v.

These commands shows the updated version. The updated version number should be visible if the update was successful.

7. If you have multiple Node versions installed on your machine, you can run the following commands to get the list of these versions.

nvm ls  or  nvm list

8. You can also set one of the versions as your default by running the following command on your command prompt.

nvm alias default <version>

//Example:
nvm alias default 16.16.0

9. When a new session starts, this version will automatically be chosen. Additionally, you can use its alias, as in the command below,

nvm use default

10. You can know the currently active Node.js version by the following command.

nvm current

11. The following command is used to switch between different Node.js versions managed by NVM.

nvm use

//Example:
nvm use 14.16.0

This will show a list of the Node.js versions that are currently installed along with an arrow (->) designating the active version.

(Before using this ‘nvm use’ command first you need to know the currently running Node.js version of your machine.)

Removing Versions

  1. If you no longer need a specific Node.js version, you can remove/ uninstall it with the following command.
nvm uninstall <version>

//Example
nvm uninstall 14.16.0

Enabling and Disabling Node.js Versions

You can enable and disable specific Node.js versions with NVM (Node Version Manager) without uninstalling them. When you want to temporarily enable or disable a specific version, these commands are helpful. It gives you a flexibility when managing your development environment and working on projects that require different Node.js versions.

  1. To enable a specific Node.js version, use the nvm enable command followed by the version number.

A version is made usable in your NVM environment when it is enabled.

nvm enable 14.16.0

2. To disable a specific Node.js version, use the nvm disable command followed by the version number.

A version will no longer be usable in your NVM environment once it has been disabled.

nvm disable 14.16.0

nvm ls can be used to see which versions are enabled and which ones are disabled. Versions that are enabled will be denoted with an ‘O’, and versions that are disabled will be denoted with an ‘X’.

NVM provides other commands and options for managing Node.js versions. You can explore them by running the following command.

nvm --help

Conclusion

Your development workflow will be flexible, convenient, and stable if you use NVM to manage Node.js versions. You can use NVM to seamlessly switch between Node.js versions, guarantee project compatibility, and take advantage of the extensive ecosystem of tools and packages made available by npm.

Utilize NVM features to give yourself the freedom to explore various Node.js versions and discover the Node.js’ full potential for your projects.

Happy Coding! 😎

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

--

--