Check out a free preview of the full JavaScript and TypeScript Monorepos course

The "Lerna Commands" Lesson is part of the full, JavaScript and TypeScript Monorepos course featured in this preview video. Here's what you'd learn in this lesson:

Mike explains that Lerna gives developers the ability to run tests concurrently, and reviews the most useful Lerna commands, namely Lerna link, bootstrap, and run.

Preview
Close

Transcript from the "Lerna Commands" Lesson

[00:00:00]
>> [BBLOCK_AUDIO] So first off, we can see that the command exists and we can see the help documentation. If we look at these, there are a couple important things that you'll you'll need to worry about as you're just getting started. One of them is bootstrap. Link you also want to know about.

[00:00:23]
So bootstrap includes linking. But bootstrap does more things. It's like a superset of yarn link. learner add so that you can think of as yarn add. It's very similar And then the others that are very important, learn to run and learn to exec. So let me go through those in order in order from lowest level to highest level.

[00:00:53]
Learner link, so as soon as we have interdependencies between our packages We're going to wanna have some of our packages discoverable in the node modules folder of another package, right so that, slack slash UI needs slack slash utils. It needs to find it in the node modules folder the way it would find any other package.

[00:01:20]
So link creates the symlinks that make that work. bootstrap does the same thing as yarn link, but it also runs yarn install and it downloads anything that might need to be downloaded.Learn a run. It's like a for loop. With a yarn script, so if you would, if you were to say, learn a run build, it would go to each package and it would build them.

[00:01:47]
And it's smarter than just the way I stated it now because it will analyse who depends on who. And it will first build the lowest level packages. And then only after that's built, it'll build things that depend on those little level packages so builds from the bottom up, which is exactly what you want in order to make sure things stay in sync.

[00:02:10]
learner exec is kind of like learn to run. It operates in the same order. However, you can run an arbitrary command So you could just run LS or you could make a directory, right? You're not running like yarn test, you're just running some sort of shell command. So those are the four I want you to pay attention to now, run, exec, link and bootstrap.

[00:02:33]
And if you wanted to learn a 5th, add, like yarn, add. Okay, it's a question about link. So yarn. We'll look at our node modules. Right now yarn already created at Schlag. And then it already has types and utils. Right? Like, do we really need link yarn is already creating the link for us.

[00:02:54]
Parents, right, good question. So, in class, we just got a question that says Look we have we have already got in our top level node modules folder. We already have slack. And we already have types in utils here. So why exactly do we need link? Again, it comes down to versioning.

[00:03:15]
Right? Like, it's possible that one package in your mono repo might use an old version of something that exists in your mono repo. It sounds weird, but it can happen. Let me show you what learner link does. That is different from what we're seeing as the yarn workspaces functionality in front of us.

[00:03:40]
I'm going to introduce a pointless dependency, right? I'm going to entangle these two packages, even though I don't strictly need to. So here's how I would do that. I would say as a dependency, let's say that the utils need the types. And I'm gonna point to 001. Now you can see that even within the local node modules Have you tails, I can now find types.

[00:04:22]
It's a subtle difference, but this would not have happened with just yarn. This is something that lerna is doing for us. All right, I'm gonna kind of take advantage of some of the benefits here. So let's say. Go to the root of my workspace. I could do, they're gonna run clean and it's going to visit each of my packages and it ran clean in two packages.

[00:04:53]
They're gonna run build And it's going to build my packages and it is not an accident that it is first going into types and then going into utils because I introduced that pointless dependency there. utils doesn't really need types but my package JSON says otherwise package JSON for you tell says, in order to work, I must have types So we got to build that first can't build utils before you have everything that it needs, so it figures out this order.

[00:05:27]
Now if I were to remove this let's see if that sibling disappears as it should. Hmm, not quite. Maybe I need to run. Later to bootstrap. Let's see if this does it.There it goes. Now that symlink goes away because now we have two equally low level dependencies. Let's see if lerna run build will choose a different order now.

[00:06:06]
It chose utils first. Actually, it didn't really do utils first. I suspect it ran them both side by side. Just judging by the amount of time this is taking. Just let's do some quick math here. point eight seconds plus point eight seconds is point eight seconds. Nope, these must have been running in parallel.

[00:06:35]
So learner understands that these things can be built in parallel. And so they will be, because no one depends on anything else. This is something that as your project gets more complicated, it would be very difficult to keep track of and it would be hard to keep any build scripts where this was explicitly defined.

[00:06:56]
It would be hard to keep them up to date. I think a lot of teams would end up just building everything sequentially and You know, that takes a lot longer. We have a lot of CPU cores these days on, you know, on new newer computers, this, you know, fans out and takes more advantage of that horizontal scaling.

[00:07:19]
We could also run yarn test And this should be more obvious because we see some results come out. It's not obvious. Now, these were definitely still run in parallel, right because the total amount of time it took was the longest leg right this 2.7 happened within the 3.2 We didn't see any output.

[00:07:42]
There's an argument you can add here called stream, which is just like any console log or any standard out, and standard error. That may happen while we're running yarn test. I wanna see that, and now you can see the interleaving of the outpu,t right? At least up at the top.

[00:08:03]
Or we're saying just just it's running these two packages very obviously, they're running in parallel. Whereas if I were to say concurrency one. Here's types doing its thing followed by you tells you a whole lot of little knobs you can turn here, especially if you have a bunch of different packages right?

[00:08:22]
The ability to run all your tests in parallel where possible. It's a big deal. So that's learner

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now