nvm (Node Version Manager): Install Guide & Cheat Sheet

Avatar

By squashlabs, Last Updated: Nov. 8, 2023

nvm (Node Version Manager): Install Guide & Cheat Sheet

Intro

"nvm" stands for Node Version Manager, and it is a tool that allows developers to easily manage multiple versions of Node.js on a single machine. Node.js is a JavaScript runtime that allows developers to run JavaScript code on the server-side, and it is widely used in web development for building server-side applications, APIs, and other back-end services.

One of the challenges of working with Node.js is that different projects may require different versions of Node.js, and sometimes these versions may not be compatible with each other. This can lead to conflicts and issues when trying to run multiple Node.js projects on the same machine.

This is where "nvm" comes in handy. It allows developers to easily switch between different versions of Node.js, install new versions, and manage dependencies associated with each version. "nvm" provides a simple command-line interface that allows developers to switch between Node.js versions with just a few commands, making it convenient and efficient to work with multiple Node.js projects that require different versions.

For example, if you have one project that requires Node.js version 12 and another project that requires Node.js version 14, you can use "nvm" to switch between these versions depending on which project you are working on. "nvm" also provides commands to list all installed Node.js versions, set a default version, and manage global and local Node.js packages.

In summary, "nvm" is a powerful tool that helps developers manage multiple versions of Node.js on a single machine, making it easier to work with different Node.js projects that require different versions, and avoiding conflicts and compatibility issues.

Related Article: Creating Web Workers with NextJS in JavaScript

Installation and First Steps

Here's a comprehensive cheat sheet for using "nvm" (Node Version Manager) to manage multiple versions of Node.js on your machine:

1. Installing "nvm"

Open your terminal and run the following command to install "nvm" on your machine:

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

Follow the on-screen instructions to complete the installation.

2. Managing Node.js versions

To install a specific version of Node.js, run the following command:

nvm install <version>

For example, to install Node.js version 14, you can run:

nvm install 14

To switch to a specific version of Node.js, run the following command:

nvm use <version>

For example, to switch to Node.js version 12, you can run:

nvm use 12

To set a default version of Node.js, run the following command:

nvm alias default <version>

For example, to set Node.js version 16 as the default version, you can run:

nvm alias default 16

To list all installed Node.js versions, run the following command:

nvm ls

Related Article: JavaScript Prototype Inheritance Explained (with Examples)

3. Managing global and local packages

To install a package globally for a specific Node.js version, run the following command:

nvm exec <version> npm install -g <package>

For example, to install the "express" package globally for Node.js version 14, you can run:

nvm exec 14 npm install -g express

To install a package locally for a specific Node.js version, run the following command in your project directory:

nvm use <version>
npm install <package>

To list all global packages for the currently active Node.js version, run the following command:

nvm exec current npm list -g --depth=0

To list all local packages for the currently active Node.js version in your project directory, run the following command:

nvm use <version>
npm list --depth=0

4. Additional commands

To display the currently active Node.js version, run the following command:

nvm current

To display the default Node.js version, run the following command:

nvm alias default

To display the installed Node.js versions, including the currently active version, run the following command:

nvm ls

To uninstall a specific version of Node.js, run the following command:

nvm uninstall <version>

For example, to uninstall Node.js version 10, you can run:

nvm uninstall 10

To update "nvm" to the latest version, run the following command:

nvm upgrade

With this cheat sheet, you should now have a handy reference for using "nvm" to manage multiple versions of Node.js on your machine.

nvm on Ubuntu 22

Here is a general guide on how to install nvm (Node Version Manager) on Ubuntu:

Step 1: Update System Packages

Open a terminal and run the following command to update the system packages to the latest version:

sudo apt update

Related Article: How to Check If a String is a Valid Number in JavaScript

Step 2: Install Prerequisites

nvm requires some dependencies to be installed on your system. Run the following command to install these dependencies:

sudo apt install build-essential libssl-dev

Step 3: Download and Install nvm

You can download and install nvm using the curl or wget command. Choose one of the following options:

Option 1: Using curl

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

Option 2: Using wget

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

Note: You can check for the latest version of nvm by visiting the official GitHub repository: https://github.com/nvm-sh/nvm

Step 4: Add nvm to Bash Profile

nvm needs to be added to your Bash profile to enable it every time you open a new terminal window. Run the following command to add nvm to your Bash profile:

echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc
echo '[ -s "$NVM_DIR/nvm.sh" ] &amp;&amp; \. "$NVM_DIR/nvm.sh"' &gt;&gt; ~/.bashrc
echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"' >> ~/.bashrc

Step 5: Source Bash Profile

To apply the changes made in the Bash profile, run the following command:

source ~/.bashrc

Related Article: How to Generate a GUID/UUID in JavaScript

Step 6: Verify nvm Installation

To verify that nvm has been successfully installed, run the following command:

nvm --version

You should see the version number of nvm, indicating that it has been installed successfully.

nvm on CentOS

Here are the step-by-step instructions to install nvm (Node Version Manager) on CentOS:

1. Open a terminal window on your CentOS system.

2. Install the required dependencies, including curl and git, by running the following command:

sudo yum install curl git

3. Download the nvm installation script from the official GitHub repository using curl:

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

Note: You can check for the latest version of nvm on the official GitHub repository and replace the version number in the URL above accordingly.

4. The installation script will download and install nvm in your home directory (~/.nvm) and update your shell profile file (e.g., ~/.bashrc) to include nvm settings.

5. Close and reopen your terminal or run the following command to apply the changes to your current terminal session:

source ~/.bashrc

6. Verify nvm installation by running the following command:

nvm --version

You should see the version number of nvm, indicating that it has been successfully installed on your CentOS system.

With nvm installed, you can now easily manage Node.js versions on your CentOS system by using nvm commands, such as nvm install, nvm use, and nvm ls, among others. You can also set default Node.js versions, create aliases, and migrate global packages, as mentioned in the previous responses.

Note: Make sure to carefully follow the installation instructions and verify the compatibility of nvm with your CentOS version before proceeding with the installation. Always refer to the official nvm documentation for the most up-to-date and accurate installation instructions.

Advanced commands

Along with basic commands like installing, listing, and switching Node.js versions, nvm also provides several advanced commands that offer additional functionality. Here are some of the advanced commands of nvm:

1. nvm alias: This command allows you to create aliases for Node.js versions. You can give a custom name to a specific Node.js version, and then use that name instead of the version number in nvm commands. For example, you can create an alias default for a specific Node.js version, and then use nvm use default to switch to that version.

2. nvm which: This command displays the path of the currently active Node.js binary, which can be useful to verify the version of Node.js that is currently being used by your system.

3. nvm reinstall-packages: This command reinstalls global npm packages from the currently active Node.js version to another version. This can be useful when you switch to a different Node.js version and want to reinstall your global npm packages for that specific version.

4. nvm migrate: This command allows you to migrate global packages from one Node.js version to another. It can be useful when you switch to a new Node.js version and want to transfer your global npm packages from the previous version to the new one.

5. nvm version: This command displays the version of nvm that is currently installed on your system, allowing you to check for updates or verify the installed version.

6. nvm deactivate: This command deactivates the currently active Node.js version, allowing you to revert to the system-installed version of Node.js or switch to a different version managed by nvm.

7. nvm exec: This command allows you to execute a command with a specific Node.js version, without permanently switching to that version. It can be useful for running a command or script with a different Node.js version than the currently active one.

8. nvm unalias: This command allows you to remove an alias that you previously created using the nvm alias command. It can be useful when you no longer need a particular alias.

These are some of the advanced commands that nvm provides, offering additional flexibility and functionality for managing Node.js versions on your system. It's worth exploring these commands and their usage in the nvm documentation to fully leverage the power of nvm for your Node.js development workflow.

Conclusion

nvm (Node Version Manager) is a powerful tool for managing Node.js versions and dependencies, making it easier for developers to switch between different Node.js versions, install and use specific versions of Node.js, and manage global and local packages. In this guide and cheat sheet, we have covered the installation process of nvm, basic commands for managing Node.js versions, as well as advanced commands for managing aliases, setting default versions, and migrating global packages.

By using nvm, developers can ensure that their Node.js applications are running on the desired version of Node.js, which is crucial for maintaining compatibility and stability. With its flexibility and ease of use, nvm has become a popular choice among Node.js developers for managing Node.js versions in development, testing, and production environments.

We hope this guide and cheat sheet has provided you with a comprehensive understanding of nvm and its usage. Whether you are a beginner or an experienced Node.js developer, nvm can greatly simplify your Node.js version management process and improve your workflow. Don't forget to refer back to this cheat sheet whenever you need a quick reference for nvm commands.

How to Use Classes in JavaScript

Classes are an essential part of JavaScript programming, allowing you to organize and structure your code more efficiently. In this article, we will … read more

How to Reverse a String In-Place in JavaScript

This article provides a step-by-step guide to reversing a string in-place using JavaScript. Learn how to convert the string into an array, reverse th… read more

How to Add a Key Value Pair to a Javascript Object

Guide on adding a key value pair to a Javascript object using simple code examples. This article covers the two common notations for adding key value… read more

Executing a Local NextJS Build in JavaScript

Learn the steps to perform a NextJS build locally in JavaScript without complications. Discover how to install Next.js, set up a development environm… read more

How to Shuffle a JavaScript Array

This article provides a guide on how to shuffle a JavaScript array. Learn two methods: using the Fisher-Yates Algorithm and the Array.sort() method. … read more

Grouping Elements in JavaScript Using 'Group By'

In this article, we will explore how to use the 'Group by' function in JavaScript for data aggregation. We will cover the purpose of 'Group by', prov… read more

AngularJS: Check if a Field Contains a MatFormFieldControl

AngularJS developers often encounter the need to ensure that their Mat Form Field contains a MatFormFieldControl. This article provides guidance on h… read more

Advanced DB Queries with Nodejs, Sequelize & Knex.js

Learn how to set up advanced database queries and optimize indexing in MySQL, PostgreSQL, MongoDB, and Elasticsearch using JavaScript and Node.js. Di… read more

Creating Dynamic and Static Pages with Next.js

Developing dynamic and static web pages using Next.js framework is made easy with the step-by-step instructions provided in this article. From learni… read more

How To Get A Timestamp In Javascript

In this article, you will learn how to get a timestamp in JavaScript. Whether you need to obtain the current timestamp or convert a date to a timesta… read more