javascripttooling

Hassle-free Node Environment Management with Volta

Aftab Alam

Aftab AlamMarch 14, 2021

5 min read–––

Aftab Alam

Javascript tooling has come a long way since the very humble beginnings, or the place where the only realm known to Javascript used to be the browser's window. The advent of NodeJS as a runtime to execute Javascript outside of the browser brought it in the league of other languages with more mature tooling around them like Ruby, Python, etc. When you work with multiple projects, in Javascript or any other language it becomes very crucial to manage the project dependencies in a very effective manner. Different projects rely upon community packages(libraries and plugins) that are somewhat bound to the language runtime version they are written in mind with.

This need created a space for tools that can let you manage multiple versions of runtime in a system in a non-conflicting way. For example, if your blog project is dependant on Node's latest 14.x version and your e-commerce project is dependant on 12.x you can have both installed in your system, to enable the environment each of your projects needs conditionally. In the lines of rvm and pyenv the Node ecosystem produced tools like nvmn that let you manage multiple Node versions and other dependencies your projects relied upon effortlessly.

These tools are great aids and have served us well in taming the complexity of Javascript tools your projects rely upon. Doing something like switching NodeJs versions as per your projects is just a command away but changing the state of your installed software when switching between projects could still be work and full of hassles. These limitations become more apparent when this seemingly simple job needs to scale to support teams, which could be on different platform types.

It's 2021! Wouldn't it be good if we can have a tool that can

  • Infer and use a project's context — understands what node runtime and tools a project needs and sets it up so that it only sees what it needs when a developer navigates into it
  • make this behavior portable and re-usable across multiple platforms, and machines through some good convention or configuration

Meet Volta!

What is Volta?

Volta is a brand new, blazingly fast, cross-platform, tool that takes the pain out of managing multiple javascript command-line tools such as node, npm, yarn, or executables shipped as part of JavaScript packages in a very reliable manner. Currently, at 1.0.2, Volta is developed by Chuck Pierce, a Linkedin employee.

Installation and Usage

It's a single binary that can be installed with a single command:

# install Volta
curl https://get.volta.sh | bash

on most Unix systems and with a dedicated installer for Windows.

Once Setup, node can be installed by issuing a simple

# install latest node (as global default)
volta install node

# or, a specific version
volta install node[@version]

# start using Node
node

The above command will set a global default version of node and will make sure the specified version is available to all the projects that need it.

If you want to then set a different version for a specific node project, you can use

volta pin node[@version]

from within the project's directory. For example, issuing the above command like

volta pin node@14.15.4

will add the volta key to that project’s package.json

"volta": {
    "node": "14.15.4"
}

After that, if you run node inside the project, it will use the local version defined in the project, and if you run node anywhere else, it will use the global default you picked with volta install

If Volta notices volta in your project's package.json it kinda creates a realm local to your project by context detection, if not the global settings apply.

Besides pinning a locally needed node's runtime version, the same pin command can be used to install and pin global package managers like yarn

volta install yarn
volta pin yarn
  • Only npm, yarn, and npx is supported currently, with support for pnpm and pnpx coming soon

Pinning helps in producing reproducible environments for collaborators. Everyone who uses Volta to work on the project automatically gets the same version you selected.

Amazingly, once Volta is the manager for your default Node.js runtime, installing a command-line tool globally with your package manager also adds it to your toolchain.

yarn global add typescript

Exactly in the way you'd expect a new tool managing your global packages to work.

Volta's toolchain keeps track of where you are, respects the settings of the project you’re working on, and will give preference to the local version of a package irrespective of its presence at the global level.

cd /path/to/project-using-typescript-3.9.4
tsc --version # 3.9.4

cd /path/to/project-using-typescript-4.1.5
tsc --version # 4.1.5

Closing Thoughts

The very simple premise that you don't have to manually keep track of the node versions(and different related CLI packages) that are installed, available and are going to be needed as you go about building, experimenting, and supporting Javascript projects is sort of groundbreaking IMHO. Volta is a hassle-free, efficient and reliable way to reproduce the ceremonial work needed to get done before you get started with your development, as well as to support a conflict-free experience as your project grows. It's also ready for workspaces and has a very elegant way to allow for private NPM repositories. Volta itself is a blazing fast single-file executable written in Rust, and thus is very easy to set up, configure and remove. All of these features and characteristics and the fact that it's already being used at Linkedin, Salesforce, etc. make Volta a compelling choice for addition to your development workflows in 2021 and beyond.

Thank you for being till the end 🙌 . If you enjoyed this article, or learned something new, please take a second to tweet this article or share on LinkedIn so others can discover it. Or add to the discussion on Twitter.

Tweet about this post(by clicking the button above) or like/re-tweet one of the tweets that shares this article, and it will show up here eventually