How to Use the Command 'nvm' (with examples)
- Windows
- December 17, 2024
The Node Version Manager (nvm) is an incredibly useful tool for developers who need to manage multiple versions of Node.js on their systems. This command-line utility allows users to easily install, switch, and manage Node.js versions without any significant hassle, ensuring that your development environment can accommodate various project requirements. Whether you’re working with legacy code that relies on an older version of Node.js, or exploring new features in the latest release, nvm provides a streamlined solution to manage your Node.js versions with ease.
Use case 1: Install a Specific Version of Node.js
Code:
nvm install node_version
Motivation:
Developers often work on various projects that may depend on different versions of Node.js. Having the ability to quickly install a specific version of Node.js ensures that you can match the exact environment required by each project, which is critical for testing and development continuity. This flexibility prevents compatibility issues that might arise from changes in Node.js between versions.
Explanation:
nvm
: The command-line interface of Node Version Manager.install
: A sub-command that triggers the installation process of a specific Node.js version.node_version
: This argument specifies the exact version of Node.js you wish to install. It can be a version number like “12.8”, “14.17.3”, or a label like “stable”.
Example Output:
Downloading node.js version 14.17.3 (64-bit)...
Complete
Creating C:\nvm\v14.17.3
...
Installation complete. If you want to use this version, type
nvm use 14.17.3
Use case 2: Set the Default Version of Node.js
Code:
nvm use node_version
Motivation:
Setting the default version of Node.js is essential when you frequently work with a specific version and want to ensure that all your executions default to this version. This way, you can avoid manually switching versions each time you start a new session, streamlining your development workflow. Setting a default version also reduces the risk of mismatches in your development environment.
Explanation:
nvm
: The command-line interface of Node Version Manager.use
: A sub-command indicating the switching to a specified Node.js version.node_version
: This argument tells nvm which version of Node.js you wish to set as the default, such as “12.8” or “v16.13.1”. Running this command requires administrative privileges to effect the change system-wide.
Example Output:
Now using node v14.17.3 (64-bit)
Use case 3: List All Available Node.js Versions and Highlight the Default One
Code:
nvm list
Motivation:
When working on multiple projects, it’s easy to lose track of the versions of Node.js that are currently installed. Listing all available versions, along with clearly identifying the current default, helps in managing these installations. It also aids in decision-making when you need to consolidate your environment or switch between versions.
Explanation:
nvm
: The command-line interface of Node Version Manager.list
: A sub-command that prompts nvm to display all installed Node.js versions on your system and highlights which one is currently set as default.
Example Output:
* 14.17.3 (Currently using)
12.8.0
10.15.0
Use case 4: List All Remote Node.js Versions
Code:
nvm ls-remote
Motivation:
Staying informed about all available Node.js versions, including the latest releases, is vital. This command provides a complete list of all Node.js versions offered remote, enabling you to quickly decide if you should upgrade or try out new features. It is particularly useful for maintaining software that uses cutting-edge tools or for ensuring compatibility with the newest standards.
Explanation:
nvm
: The command-line interface of Node Version Manager.ls-remote
: A sub-command that requests nvm to list all Node.js versions available for installation from the remote repository.
Example Output:
v16.13.1
v16.12.0
v15.14.0
v14.17.3
...
v8.17.0
Use case 5: Uninstall a Given Node.js Version
Code:
nvm uninstall node_version
Motivation:
Over time, your system may accumulate multiple versions of Node.js, some of which might no longer be necessary. Uninstalling these unused versions helps conserve disk space and keeps your development environment tidy and efficient. It also helps reduce potential confusion or errors stemming from cluttered version lists.
Explanation:
nvm
: The command-line interface of Node Version Manager.uninstall
: A sub-command directing nvm to remove a specified version of Node.js.node_version
: The version reference that you wish to uninstall, such as “10.15.0” or any other previously installed version on your system.
Example Output:
Uninstalling node v10.15.0...
done
Conclusion:
The nvm
utility simplifies the management of Node.js versions, offering straightforward commands that enhance flexibility and control within your development environment. By understanding these use cases, developers can quickly adjust to diverse project requirements, foster better testing environments, and generally improve workflow efficiency.