

Plug’n’play: replaces Node’s dependency resolution mechanic.Zero installs setups: dependencies are deduped and committed directly to the repository, making builds more reproducible and avoiding version conflicts.Yarn version 2 was released in 2020 with a lot of monorepo goodies: Yarn is an alternative package manager to npm. Workspaces: Yarn can now manage dependencies across many independent projects in the same repository.Provides more reliable installs and faster application startups. Instead of having several big node_modules folders scattered all over, we now have all dependencies compressed in one repository-level folder. We’re going to use one example repository as a guide. You can peek at the final form in the final branch.
LERNA VS YARN WORKSPACES PLUS
Web: a Next.js static website generated programmatically with the API.īoth of them ship with linting checks and unit tests, plus some integration tests to confirm that they can work together.api: provides a GraphQL API server that returns information about countries and cities of the world.The example repository contains two applications: The steps laid down here, however, should mostly work with any JavaScript application. The first thing to do is set up the workspaces. Start by ensuring that you’re running the latest Yarn version. Next, initialize the monorepo workspaces. This will generate a root-level package.json. It will also create a packages folder, where Yarn will expect the applications to be. The paths are defined in a list inside the workspaces key in the main package.json. Now, move all applications into the new folder. Yarn should pick up the package manifests in each subfolder in packages/*. Yarn will map the name of each workspace from using the application-level package.json. You can confirm that workspaces are working by running yarn workspaces list.
LERNA VS YARN WORKSPACES INSTALL
You should now have a few new files at the root of the repository: The next step is to install all the dependencies. Yarn/cache: this is your new node_modules folder. With the difference that each dependency is only downloaded once and saved in compressed state. yarn.lock: the checksum file contains the exact dependency versions installed.ĭelete the old yarn.lock as sometimes Yarn gets confused by : the Plug’n’play file provides Node a complete map of your dependencies by listing their versions and locations in the repository. $ git commit -m "finalize monorepo setup"īear in mind that, by default, Yarn does zero install setups. That means that all downloaded modules are committed to the remote repository. Therefore, the first push might take longer than usual.

If you don’t want to push the modules to, change these two lines in. What did we gain with all this work? For one thing, we now have all dependencies compressed and deduplicated, making application startup faster and reducing the space required on disk. It is now a lot more feasible to push all dependencies into the repository.

Start and test any application with: $ yarn workspace start For another, we also gained the ability to run any application directly from the repository root.
