DEV Community

Cover image for Manage Multiple Node Versions with Volta (better than nvm?)
ishan
ishan

Posted on • Updated on

Manage Multiple Node Versions with Volta (better than nvm?)

In recent years Javascript tooling and ecosystem has taken a large leap ahead. There has been lot of changes in how we develop and ship our digital products. Today, we will discuss and learn about how we can painlessly manage multiple Node version across different projects.

There are some useful tools like; nvm, snm etc which fulfills the same purpose what Volta offers of managing multiple versions of Node. But Volta is better and different we will learn how and why you should choose Volta for your upcoming project.

What is Volta?

Volta, the "Hassle-free JavaScript Tool Manager,", is an open source project to simplify the management of JavaScript environments.

Volta(https://volta.sh/) is a command line tool built using Rust which helps us to manage Node.js versions. It is built using Rust and is shipped as a static binary that can be ran on Windows and all *nix shells.

Volta makes global installs a first-class part of the JavaScript tooling experience, making them both convenient and safe to use.

Volta takes a set-it-and-forget-it approach. You "pin" the version of Node we need for our project and Volta auto-magically switches to that version anytime you are working on that project, without any action on your part. You can have multiple projects open at the same time, each with their own, different versions of Node, running simultaneously.

Doesn't this sound great! 😃

We just need is to specify tool configuration on our project using declarative syntax, and Volta handles the tactical details to properly stand-up the environment to your liking. To speed up execution, Volta will serve items from a local cache if possible.

Let's now take a look on how we can implement this in our project.

Installation

There are few number of ways how we can install Volta for our operating system of choice. Here we will cover for most Unix based systems.

curl https://get.volta.sh | bash
Enter fullscreen mode Exit fullscreen mode

The above script installs the binary in ~/.volta and adds ~/.volta/bin to your systems path inside of your ~/.bash_profile, ~/.profile, and ~/.bashrc.

export VOLTA_HOME="$HOME/.volta"
export PATH="$VOLTA_HOME/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

This is all it! We can now start using Volta to manage our Node.js.

Using Volta

We now have Volta installed in our machine. We can install node using Volta with following commands.

# Install node
volta install node
# or we can install a specific node version
volta install node@17
Enter fullscreen mode Exit fullscreen mode

After these commands we should be able to use node whenever we open our terminal. In case we want Volta to always load a specified version of node for your active package we can use pin.

volta pin node@17.0.0
Enter fullscreen mode Exit fullscreen mode

This command will store our pinned version in your package.json inside our project folder.If we look into our package.json file it looks something like this

"volta": {
  "node": "17.0.0"
}
Enter fullscreen mode Exit fullscreen mode

Each time we navigate into this project we can always find the specified version of above node installed and ready for us. Volta will automatically set your active node version to whatever is pinned.

We even can install and pin global packages like yarn or npm using Volta to make sure everyone on your team is using the same version for their global packages.

volta install npm
volta pin npm
Enter fullscreen mode Exit fullscreen mode

Below is what our package.json looks like 😍

"volta": {
  "node": "17.0.0",
  "npm": "8.0.0"
}
Enter fullscreen mode Exit fullscreen mode

Volta vs nvm

nvm exists to handle Node.js versions, what makes Volta better than nvm is it requires us to either always run commands when switching projects (nvm use) or add helpers to our shell. Instead Volta handles node versions and can set a default version to load also right inside our package.json file.

Think what's about npm or yarn? If you are struggling with package-lock.json version changes as devs move from npm v6 to v7?

NVM doesn't automatically switch your active node version to your pinned version. You have to run nvm use or install another package call AVN. AVN usually takes 2 to 6 seconds to switch versions of node. But, Volta does it automatically and usually takes less than a second 😱.

Volta has us all covered, we pinned the tool in our project so we’ll always use that version. Which is applicable for node version or npm/yarn versions as well.

Final Thoughts

Staying updated in this fast pace technological wildfire is really daunting. Building applications on a foundation of constantly updating libraries adds up complexity into our development process.

Volta defends our environment against issues like “used to work,” or “works on my local machine.”, giving us the control and automation we need to make sure the environments are configured exactly the way we want, every time.

Today, different other languages has been generously contributing to javascript ecosystem to speedup the product building process. Looking at the current trends today we can see Rust has become a major contributor in enhancing the deployment experience for Javascript. There are some of the noticeable example of JavaScript tooling that is powered by Rust:

From the trend and observation from above we can see whats happening. with this contributions into JavaScript ecosystem we are building a faster, reliable and scalable development environment for our better future ahead.

👨‍💻 Happy Coding!

Top comments (0)