volta: a global toolchain manager for javascript

2020-10-18

 | 

~4 min read

 | 

622 words

At the office, our recommended toolchain manager for Javascript is Volta. As someone who has struggled with global | packages in the past, I was very intrigued by what Volta promised: a simple, secure way to manage packages globally.

Previously, I’d used NVM to manage Node versions (but that left its own problems with global installs). Some of this I could get around by overloading the cd command to check for an .nvmrc file and automatically switch my node version. But while it worked, that never felt like an ideal solution.

If you’re like me, you’re likely migrating from NVM (if you’re not, you can skip ahead to the Install Volta step). Let’s take a look at the steps you’ll take to adopt Volta:

  1. Uninstalling NVM
  2. Installing Volta
  3. Installing global binaries via Volta
    1. Listing Installed Versions
  4. Pinning Versions To Your Projects

Uninstalling NVM

According to NVM, the easiest way to uninstall NVM is by removing the $NVM_DIR:

Note: to remove, delete, or uninstall nvm - just remove the $NVM_DIR folder (usually ~/.nvm)

We can confirm that we have that directory:

ls ~/.nvm

You’ll get one of two console outputs:

/Users/stephen/.gitconfig

or:

ls: /Users/stephen/.nvm: No such file or directory

If you get the former, we can remove it with rm:

rm -rf ~/.nvm

Install Volta

Once NVM is out of the way, we’re ready to install Volta. This can be done with a single curl command:

curl https://get.volta.sh | bash

Install Node & Other Binaries Globally

Now that Volta’s installed, we’re ready to install our packages. Let’s start with node:

volta install node

By default, Volta installs the latest (stable) versions of each package and assigns it as a default. You can have multiple versions of Node installed however (which is why it’s a suitable replacement for NVM) because we can “pin” tools within a project’s package.json (more on this in a minute).

success: installed and set node@12.19.0 as default

Listing Installed Packages

As I mentioned earlier, Volta can have multiple versions of packages installed, but each is assigned a default.

When you list the installed packages, you only see the defaults:

volta list
⚡️ Currently active tools:

    Node: v12.19.0 (default)
    npm: v6.14.8 (default)
    Yarn: v1.22.5 (default)
    Tool binaries available:
        create-mern-application (default)
        np (default)
        terminalizer (default)

See options for more detailed reports by running `volta list --help`.

To see all installed versions, specify it as the argument of the list command:

volta list node
⚡️ Node runtimes in your toolchain:

    v12.18.3
    v12.19.0 (default)
    v14.14.0

Pinning Versions

Now that we have a sense of how to install packages globally, we should understand that in order to make sure that you’re always using the right package, Volta uses a concept of “pinning”. This can be done manually by editing the package.json, but it’s easier (and safer from a fat-finger perspective) to use the CLI:

volta pin node
success: pinned node@12.19.0 (with npm@6.14.8) in package.json

This can then be verified by looking at the package.json:

package.json
{
  //...
  "volta": {
    "node": "12.19.0"
  }
}

The pin will act like an .nvmrc file alerting any collaborator which version of node should be used.

Wrap Up

I’m confident there are many more reasons to like (and likely a few to dislike) Volta. But so far, it’s been really lovely and helped me avoid many of the pain points I previously encountered with globally installed packages (though, if I’m honest, I still avoid them, preferring, instead to declare the dependency explicitly within the project).

Here’s to the continued exploration!


Related Posts
  • fem-intro-to-elm


  • Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!