Check out a free preview of the full Production-Grade TypeScript course:
The "Volta" Lesson is part of the full, Production-Grade TypeScript course featured in this preview video. Here's what you'd learn in this lesson:

Mike explains the difference between Volta and the Node Version Manager (NVM). Volta seamlessly switches between command-line tool versions depending on a project's requirements. This segment also addresses why Yarn is recommended with TypeScript and Volta's compatibility between Yarn and NPM.

Get Unlimited Access Now

Transcript from the "Volta" Lesson

>> So we got a question in the class. What is Volta and how does it relate to NVM? So both of these tools aim to allow a team to define, we're using this version of node in our project. And this is useful if you have, some things that may be legacy projects and they're using an end of life version of node.

[00:00:21] Another project is using, the latest stable release. You wanna make sure that you have the right tools for the job in each of these projects and you're not simply trying to find one global version that you use across everything you work on. So both tools try to solve that problem.

[00:00:36] The way NVM solves this problem is by managing a set of environment variables, right? And that will tell you that will alter your path effectively as you change from folder to folder, go from project to project. Volta does things a bit differently. With Volta it actually will replace node your global node command with a shim that allows it to, when invoked download the right version on the fly.

[00:01:07] So there are no setup commands and there's no overhead cuz this thing is volt has written in rust. So there's maybe like a single digit number of milliseconds added overhead instead of, running some JavaScript code or running other things as more overhead. So effectively, it's like it's really lightweight.

[00:01:25] It stays out of the way there GitHub actions written for it so it makes it really easy to use in CI. And it works with yarn as well it's not just about node. So different versions of yarn matters, especially if you get into things like yarn workspaces and monorepos.

[00:01:42] So it's just as important to solve this problem in other tools as well, and, this makes it really easy. So, One way I can demonstrate that this is this is a different animal than NVM is invoking command as if it were global. And I think what we'll see is volta will choose the correct version to run based on a global invocation that is performed within a project.

[00:02:16] So I'm gonna go out of my project folder and I'm gonna run TSC and let's see what version we have. All right now, just temporarily I'm going to downgrade my version of the TypeScript compiler. Let's do the figure 3.8 release. Then we're gonna install that. And what we should see is if I go back here and I see the version of the TypeScript compiler I'm using, there's 403 and if I go within my project, let's see if this works.

[00:02:57] There we go. So what, we see here is I can just run TSC I can forget volta is even installed on my system and it's choosing the latest 3.x release, right, that little carret means I flip forward to 3.9. If you're using NVM, it wouldn't have solved the problem in this way would only be worried about node and nodes version.

[00:03:20] But here I can have a global installation, I can run it as if it's global. But because I'm in the context of a project and that project has a specified version of the typescript compiler, has a specified version of node and yarn, things are gonna just kinda wire up.

[00:03:41] And, my intent is, I wanna run this in this workspace, and it should be this version even though I'm running TSC. I don't have to run yarn TSC, for example, that's a big difference. I'm gonna undo this change real quick.
>> And in chat people are asking if you can use NVM instead of yarn.

>> All right, so I would highly recommend using yarn you're welcome to try NVM if you have issues, then my first the first thing I'm gonna ask you to do is try to use yarn. Here's why. I have a yarn.lock file here. So I've specified some versions of dependencies, particularly if you're watching a recorded version of this course, there may be newer versions of these dependencies available, if you were to just install all of them fresh.

[00:04:38] This lock file is very important, because what works for me is the exact versions in that lock file and only if you're using yarn, will that lock file be honored. If you use NVM, you're just gonna get the latest of everything. So I would really recommend that you use yarn.

[00:04:57] If you have a strong preference, the good news is yarn is not really an important aspect of what we're gonna be working with today.
>> Yeah, I think the question was more around yarn and volta or sorry NVM and volta, if you're wanting to use volta with NVM.

>> Understood, all right, so the question is, does volta do the same thing for NVM that it does for yarn? The answer's yes.