I. Overview

Node Version Manager (NVM) is a useful tool for managing and switching between multiple Node.js versions. In this blog post, we’ll cover the most commonly used features of NVM, installation instructions for different platforms, and why it’s a valuable tool for developers.

II. Installation

macOS

Using Homebrew:

brew install nvm
mkdir ~/.nvm

Add the following lines to your .bash_profile, .zshrc, or other shell configuration file:

export NVM_DIR="$HOME/.nvm"
[ -s "$(brew --prefix)/opt/nvm/nvm.sh" ] && . "$(brew --prefix)/opt/nvm/nvm.sh" # This loads nvm
[ -s "$(brew --prefix)/opt/nvm/etc/bash_completion" ] && . "$(brew --prefix)/opt/nvm/etc/bash_completion" # This loads nvm bash_completion

Linux and other Unix-based systems

Using curl:

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

Or using wget:

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

This script will clone the NVM repository to ~/.nvm and add the necessary lines to your shell configuration file (.bashrc, .zshrc, etc.).

III. Usage

1. Listing available Node.js versions

To see the list of available Node.js versions, run:

nvm ls-remote

2. Installing a specific Node.js version

To install a specific version, use the nvm install command followed by the version number:

nvm install 14.17.0

3. Listing installed Node.js versions

To see the list of installed Node.js versions, run:

nvm ls

4. Switching between Node.js versions

To switch to a specific Node.js version, use the nvm use command followed by the version number:

nvm use 14.17.0

5. Setting a default Node.js version

To set a default version for new shell sessions, use the nvm alias command:

nvm alias default 14.17.0

6. Uninstalling a Node.js version

To uninstall a specific Node.js version, use the nvm uninstall command followed by the version number:

nvm uninstall 14.17.0

7. Installing the latest LTS (Long Term Support) version

To install the latest LTS version, run:

nvm install --lts

8. Updating an installed Node.js version

To update an installed version to the latest patch, use the nvm reinstall-packages command:

nvm install 14.17.0 --reinstall-packages-from=14.16.0

9. Running a script with a specific Node.js version

To run a script using a specific Node.js version without switching the active version, use the nvm exec command:

nvm exec 14.17.0 node script.js

10. Running a script with a specific Node.js version

To run a command using a specific Node.js version without switching the active version, use the nvm run command:

nvm run 14.17.0 --version

IV. Conclusion

NVM is a powerful tool that allows developers to manage multiple Node.js versions with ease. It enables easy switching between Node.js versions, making it simple to test applications across different environments or work on multiple projects with different Node.js requirements.

With the most commonly used features covered in this blog post, you should now be able to install NVM on your system, manage Node.js versions, and use the tool effectively. Happy coding!