Managing Node.js Versions with NVM: A Beginner-to-Advanced Guide

Table of Content
- What is NVM?
- Installing NVM
- Step 1: Install NVM
- Step 2: Verify Installation
- Using NVM: Common Commands
- 1. List Available Node.js Versions
- 2. Install a Specific Version
- 3. List Installed Versions
- 4. Switch Between Versions
- 5. Set a Default Node.js Version
- 6. Run a Specific Node.js Version Temporarily
- 7. Uninstall a Node.js Version
- NVM and `.nvmrc` for Project-Specific Versions
- Steps:
- Example Workflow
- Best Practices for Using NVM
- Conclusion
Managing different Node.js versions across projects can be a challenge. NVM (Node Version Manager) simplifies this by allowing you to install, switch, and manage multiple versions effortlessly. In this guide, we’ll explore how to install NVM, use it effectively, and adopt best practices to streamline your workflows.
This guide is designed for developers of all levels, with actionable insights and examples to help you master NVM.
What is NVM?
NVM is a version manager for Node.js that lets you:
- Install and switch between multiple Node.js versions.
- Set a default Node.js version for global use.
- Use project-specific Node.js versions.
By leveraging NVM, you can avoid version conflicts between projects and ensure your development environment aligns with production.
Installing NVM
Step 1: Install NVM
Run the following command to install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
For Wget users:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.4/install.sh | bash
Note: Replace
v0.39.4
with the latest version from NVM's GitHub page.
Step 2: Verify Installation
Restart your terminal or run:
source ~/.nvm/nvm.sh
Verify installation:
nvm --version
If you see the version number, NVM is installed and ready to use!
Using NVM: Common Commands
1. List Available Node.js Versions
See all available Node.js versions:
nvm ls-remote
2. Install a Specific Version
Install a specific version of Node.js:
nvm install <version>
Example: Install Node.js 16.20.0:
nvm install 16.20.0
Install the latest LTS version:
nvm install --lts
3. List Installed Versions
List installed Node.js versions:
nvm ls
4. Switch Between Versions
Switch to a different Node.js version:
nvm use <version>
Example:
nvm use 18.0.0
5. Set a Default Node.js Version
Set a default version for all terminal sessions:
nvm alias default <version>
Example:
nvm alias default 16.20.0
6. Run a Specific Node.js Version Temporarily
Run a specific version for a single command:
nvm run <version> <script>
Example:
nvm run 14.17.0 my-app.js
7. Uninstall a Node.js Version
Remove a Node.js version:
nvm uninstall <version>
Example:
nvm uninstall 14.17.0
NVM and .nvmrc
for Project-Specific Versions
Use a .nvmrc
file to enforce a specific Node.js version for a project.
Steps:
- Create a
.nvmrc
file in your project directory. - Add the desired Node.js version (e.g.,
16.20.0
). - Run:
nvm use
NVM will automatically read the .nvmrc
file and switch to the specified version.
Example Workflow
Scenario:
You’re working on two projects:
- Project A requires Node.js 14.
- Project B requires Node.js 18.
Steps:
-
Install the required versions:
nvm install 14 nvm install 18
-
For Project A:
-
Navigate to the project folder.
-
Create a
.nvmrc
file with:14
-
Run:
nvm use
-
-
For Project B:
-
Navigate to the project folder.
-
Create a
.nvmrc
file with:18
-
Run:
nvm use
-
NVM ensures the correct version is used in each project.
Best Practices for Using NVM
- Always Use LTS Versions for Stability: LTS versions are production-ready and stable for most applications.
- Use
.nvmrc
for Project Consistency: This ensures all team members use the same Node.js version. - Regularly Update NVM and Node.js Versions: Benefit from the latest features and security fixes.
- Test Applications on Different Versions: Use NVM to test your application on multiple Node.js versions for compatibility.
Conclusion
NVM is a powerful tool for managing Node.js versions, especially for multi-project setups or legacy codebases. By mastering NVM commands and best practices, you can optimize your development workflow and minimize compatibility issues.
Start using NVM today and supercharge your Node.js projects! 🚀