DEV Community

Cover image for Have you heard of Volta?
Jamie Davenport
Jamie Davenport

Posted on • Originally published at jamiedavenport.dev

Have you heard of Volta?

Welcome to the first post in my Have you heard of series where I find new and niche technologies then try to explain them as simply as possible. I'll try to explain the problems that they solve, compare them to competitors and figure out who, if anyone, should use these tools.

This post is all about Volta, enjoy!

GitHub logo volta-cli / volta

Volta: JS Toolchains as Code. ⚡

Volta is a "the hassle-free JavaScript tool manager". What does that mean? Well, it helps you to manage multiple versions of node, npm and yarn for your projects and conveniently switch between them (hence "hassle-free").

It can be installed with a single line and comes with automatic setup and completions for popular shells. Go ahead, try it now:

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

What's the problem

It's the age-old problem of "it works on my machine". This occurs frequently when multiple people are working on a single project such as in a large enterprise or open-source.

As a developer, you probably have multiple projects that you work on. Each might depend on a different toolchain to the other. How do you manage all these different toolchain versions? It would be funny if the answer wasn't Volta.

The solution

Volta provides two important commands that solve this problem: volta install and volta pin.

First, volta install, it does what it says on the tin. It installs a specific version of a tool globally e.g. volta install node@12. This becomes the default on your machine which is used as a fallback for any projects that don't specify a version.

Secondly, volta pin allows you to set a version of a tool for the current project e.g. volta pin node@14. This gets stored in the package.json and is shared between all developers.

// package.json
"volta": {
  "node": "14.15.4",
  "yarn": "1.22.5"
}
Enter fullscreen mode Exit fullscreen mode

Here's the cool part, Volta automatically switches your toolchain whenever you change directory 🤯

Automatic toolchain switching example

If you don't have the required version installed, then Volta installs it for you. This happens whenever you try to use the tool for the first time.

Automatic installation example

Example

Here's a couple of projects that I've used Volta with:

I've also found that Volta works well in CI (example), there's even a Github Action for it 🥳

Meet the competition

NVM

NVM is a popular tool for managing multiple Node versions on a single machine. I've used NVM for a long time before now, and it's served me well.

NVM can detect the node version for a project using a .nvmrc file, but you have to manually call nvm use to switch versions or manually configure your shell to do so.

Volta also feels a lot more responsive, this is my opinion, and I'd suggest trying out both.

Docker

When you think about solving the "it works on my machine" problem, you probably think of Docker. And you should, Docker is awesome. It solves the problem, works great with CI and containers are a popular deployment target (ECS & Kubernetes).

However, it does have a much greater learning curve than Volta, and the development workflow is a lot slower, with time spent waiting for Dockerfiles to build. If you're not already using containers in your workflow, I'd suggest using Volta!

For the enterprise

Volta makes a great tool for any solo developer but it also fits in with large organisations. In addition to the problems we've already discussed, Volta also makes onboarding a breeze:

  1. Install Volta
  2. Clone projects
  3. Profit 🤩

Volta can be configured to fetch binaries from other places such as your company's private registry. It even includes support for monorepos! That thing that all the cool companies are using.

Community

We're all in this together 🎶 and Volta is no exception.

We've reached the end

I've tried to describe Volta very briefly, and from a high level, so much of the specifics have (deliberately) been glossed over. If you want to learn more about Volta, see their top-quality documentation.

If you've enjoyed this post then please let me know on Twitter. Feedback is always appreciated.

Have a great day ❤️

Discussion (1)

Collapse
zalithka profile image
Andre Greeff • Edited on

this post definitely needs more exposure! I believe you've just helped me get my head around Volta. (:

I'm so accustomed to explicitly installing Node runtimes, that I completely missed it at first; Volta works because we don't explicitly tell it to install a version..

bear with me for a moment here, because it was while reading a specific section that I imagined a "typical workflow" using Volta, and it all just suddenly made perfect sense:

First, volta install, it does what it says on the tin. It installs a specific version of a tool globally e.g. volta install node@12. This becomes the default on your machine which is used as a fallback for any projects that don't specify a version.

Secondly, volta pin allows you to set a version of a tool for the current project e.g. volta pin node@14. This gets stored in the package.json and is shared between all developers.

Here's the cool part, Volta automatically switches your toolchain whenever you change directory 🤯

....and with that knowledge, I understand volta install is to be used sparingly! as per the documentation, "The volta install command will set your default version of a tool."

so in a nutshell:

  • volta install node to set your environment default runtime version
  • volta pin node to set a custom runtime for a specific project
    • with the engines version range check..
  • node/npm/npx away to your hearts' content..

hmm.. I really like this idea! oh well. \O.o/ I'll miss nvm.. might even still use it from time to time, a simple omz custom plugin does do auto version changing on directory navigation, but even with that convenience, each installed version has it's own list of "global packages"...

is npm a victim of it's own success then, due to it's infamous node_modules folder..?? (and all the things that depend on it, of course) lol. but I digress..

regardless, I believe the engines field in a project's package.json will work well enough to ensure non-Volta users don't get left in the cold.

so thank you again! this post was an awesome find.. (: