DEV Community

Cover image for Mastering Node.js Development with Node Version Manager (NVM)
Sayuj Sehgal
Sayuj Sehgal

Posted on

Mastering Node.js Development with Node Version Manager (NVM)

As a Node.js developer, you might have encountered situations where different projects require different versions of Node.js. Managing multiple versions of Node.js can be a hassle, but Node Version Manager (NVM) simplifies this process. In this blog post, we'll explore what NVM is, how to install it, and how to use it effectively.

What is Node Version Manager (NVM)?

Node Version Manager, commonly known as NVM, is a handy tool that allows developers to install and switch between different versions of Node.js effortlessly. This flexibility is essential for developers working on multiple projects that may depend on different versions of Node.js. With NVM, you can ensure that each project uses the correct Node.js version without interfering with each other.

Why Use NVM?

1. Multiple Node.js Versions:

NVM lets you maintain and switch between multiple Node.js versions on your machine. This is particularly useful if you're working on legacy projects that require older versions of Node.js.

2. Easy Updates:

Keeping Node.js up to date is crucial for security and performance. NVM makes it easy to update Node.js to the latest version without disrupting your existing setup.

3. Environment Isolation:

Different projects often have different dependencies and requirements. NVM ensures that each project can run in its environment with the appropriate Node.js version, avoiding conflicts.

Installing NVM

Before you can start using NVM, you need to install it. Here’s a step-by-step guide:

On macOS/Linux:

  1. Open Terminal.

  2. Run the installation script:

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

Enter fullscreen mode Exit fullscreen mode
  1. Update your shell profile: Add the following lines to your ~/.bashrc, ~/.zshrc, or ~/.profile file:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Enter fullscreen mode Exit fullscreen mode
  1. Source your profile:
source ~/.bashrc
# or
source ~/.zshrc
# or
source ~/.profile

Enter fullscreen mode Exit fullscreen mode

On Windows:

For Windows users, it's recommended to use nvm-windows. You can download it from the nvm-windows GitHub repository and follow the installation instructions provided there.

Using NVM

Once you have NVM installed, using it is straightforward. Here are some common commands:

Installing a Node.js Version

To install a specific Node.js version, use:

nvm install <version>
# Example:
nvm install 16.14.0

Enter fullscreen mode Exit fullscreen mode

Listing Installed Versions

To see all installed Node.js versions, use:

nvm ls

Enter fullscreen mode Exit fullscreen mode

Switching Node.js Versions

To switch to a different installed Node.js version, use:

nvm use <version>
# Example:
nvm use 14.17.0

Enter fullscreen mode Exit fullscreen mode

Setting a Default Node.js Version

To set a default Node.js version to use in new shells, use:

nvm alias default <version>
# Example:
nvm alias default 16.14.0

Enter fullscreen mode Exit fullscreen mode

Uninstalling a Node.js Version

To uninstall a Node.js version, use:

nvm uninstall <version>
# Example:
nvm uninstall 12.18.3

Enter fullscreen mode Exit fullscreen mode

Advanced NVM Usage

Running a Specific Node.js Version Temporarily

If you need to run a specific Node.js version for a single command, you can prefix your command with nvm run:

nvm run 14.17.0 <your_command>
# Example:
nvm run 14.17.0 node -v

Enter fullscreen mode Exit fullscreen mode

Listing Available Node.js Versions

To see a list of all available Node.js versions that can be installed, use:

nvm ls-remote

Enter fullscreen mode Exit fullscreen mode

Installing Node.js with LTS (Long Term Support)

For stability, it's often best to use LTS versions of Node.js. To install the latest LTS version, use:

nvm install --lts

Enter fullscreen mode Exit fullscreen mode

Conclusion

Node Version Manager (NVM) is an indispensable tool for any Node.js developer. It simplifies the management of multiple Node.js versions, ensuring that you can work on different projects with ease and confidence. By using NVM, you can keep your development environment clean and organized, avoid version conflicts, and stay up-to-date with the latest Node.js releases.

Mastering NVM will undoubtedly enhance your development workflow. Give it a try, and you'll wonder how you ever managed without it.

Happy Coding!!

Top comments (0)