How to use the command nvm (with examples)
- Windows
- December 25, 2023
NVM is a command-line tool that allows you to easily install, uninstall, or switch between different versions of Node.js. The tool supports various version numbers and labels like “stable” or “system”. It is especially useful for developers who need to work on different projects with different Node.js requirements.
Use case 1: Install a specific version of Node.js
Code:
nvm install node_version
Motivation:
Installing a specific version of Node.js is necessary when you need to work on a project that requires a specific version. By using NVM, you can easily switch between different versions and avoid conflicts between different projects.
Explanation:
nvm
is the command to interact with NVM.install
is the command to install a specific version of Node.js.node_version
is the version number or label of the Node.js version you want to install. For example, you can usenvm install 12.8
to install version 12.8 ornvm install stable
to install the latest stable version.
Example output:
Downloading nodejs version 12.8.0 (64-bit)...
Complete
Creating C:\Program Files\nodejs environment variable...
Downloading npm version 6.14.4...
Complete
Use case 2: Set the default version of Node.js
Code:
nvm use node_version
Motivation:
Setting the default version of Node.js is useful when you want to use a specific version as your default version for all projects. This eliminates the need to specify the version every time you start a new project.
Explanation:
nvm
is the command to interact with NVM.use
is the command to change the active version of Node.js.node_version
is the version number or label of the Node.js version you want to use. For example, you can usenvm use 14.17.3
to set version 14.17.3 as the default.
Example output:
Now using node v14.17.3 (64-bit)
Use case 3: List all available Node.js versions
Code:
nvm list
Motivation:
Listing all available Node.js versions is helpful when you want to see which versions are installed on your system and which one is currently active. This information is useful for managing your Node.js environment.
Explanation:
nvm
is the command to interact with NVM.list
is the command to display a list of all installed Node.js versions.- This command does not require any additional arguments.
Example output:
* v14.17.3 (Currently using)
v12.22.5
v10.24.1
Use case 4: List all remote Node.js versions
Code:
nvm ls-remote
Motivation:
Listing all remote Node.js versions is useful when you want to see the available versions that you can install. This information is crucial when you need to install a specific version that is not already installed on your system.
Explanation:
nvm
is the command to interact with NVM.ls-remote
is the command to list all available Node.js versions that can be installed.- This command does not require any additional arguments.
Example output:
...
v14.17.3
v14.17.4
v14.17.5
v16.10.0
v16.11.0
v16.12.0
v16.13.0
v16.13.1
...
Use case 5: Uninstall a given Node.js version
Code:
nvm uninstall node_version
Motivation:
Sometimes, you may need to uninstall a specific version of Node.js to free up disk space or remove unnecessary versions. NVM allows you to easily uninstall any installed version.
Explanation:
nvm
is the command to interact with NVM.uninstall
is the command to remove a specific version of Node.js.node_version
is the version number or label of the Node.js version you want to uninstall.
Example output:
Uninstalling node v12.22.5 (64-bit)...
Complete
Conclusion:
In this article, we have covered several useful use cases of the nvm
command. With NVM, you can easily manage different versions of Node.js, such as installing specific versions, setting the default version, listing available versions, and uninstalling unnecessary versions. This tool is especially valuable for developers working on multiple projects with different Node.js requirements.