Yarn in Depth: Why & How

Rate this content
Bookmark

Since 2017 Yarn proved itself a pillar of JavaScript development incubating numerous features our ecosystem now heavily relies on. As years passed, as competitors improved, so did Yarn, and it's now time today to dive into the features and tradeoffs that make Yarn a truly unique gem of the JavaScript ecosystem.

This talk has been presented at DevOps.js Conf 2021, check out the latest edition of this JavaScript Conference.

FAQ

Yarn is a package manager for JavaScript that extends its capabilities to act as a project manager, allowing you to manage scripts, split applications into standalone modules, manage release cycles, monitor script usage, and enforce standards across projects.

Yarn's development is guided by core values such as community contribution, soundness (ensuring the application alerts you to mistakes), and promoting good practices in JavaScript development.

Yarn enhances developer experience by providing default behaviors that simplify command usage, promoting learning and better engineering practices, and integrating features that streamline project management and deployment processes.

Yes, Yarn is versatile enough to be used in both open source and internal projects, offering tools and features like Release Workflow and Zero Installs that support efficient project management and development in various environments.

Yarn transitioned to a modular architecture where the core functionality is separated into distinct modules. This allows for independent development and integration of features, making Yarn extendable and adaptable to specific needs.

As an open source project, Yarn faces challenges like ensuring sustainability and managing a large community of contributors. They address these by relying on community contributions and exploring sponsorship programs to fund their development efforts.

Yarn uses a novel approach like Plug'n'Play (PnP) and Zero Installs to handle dependencies efficiently. PnP avoids node_modules directories, while Zero Installs caches the project's dependencies within the repository for quick access and switching.

Maël Nison
Maël Nison
33 min
01 Jul, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Yarn is not just a package manager, it intends to be a project manager with a focus on simplicity and a good developer experience. Yarn's impact on workflows and project management has been positive, improving scalability and release management. It offers features like local fixes, compressed packages, and sharing packages between projects. Yarn's infrastructure and extensive testing ensure compatibility and catch regressions. Yarn is modular, with plans for version 3 and a more powerful plugin ecosystem. The choice between npm and Yarn depends on the project's configuration.

1. Introduction to Yarn and Core Values

Short description:

Today, we're going to talk about what Yarn is and what it can bring you. Yarn is not just a package manager, it intends to be a project manager. It lets you manage scripts, split your application into standalone models, and more. We rely on our contributors to make Yarn sustainable and see it as a project. Soundness is a key value when merging PRs, ensuring that Yarn detects errors and doesn't make uncontrolled assumptions.

Hi everyone, for those who don't know me, my name is Mael. I currently work at Datadog and I've been leading Yarn's development for a few years now. Today, we're going to extensively talk about what it is and what it can bring you. While we won't be able to go into each and every single little quality of life improvements it offers, I hope that by the end of this talk, you will have a better idea of what makes this project unique in our ecosystem.

First, we should discuss what's actually Yarn. If you ask anyone, they will likely tell you that Yarn is a package manager for JavaScript, and they would be mostly right. But it's only part of the story. Yarn is not just a package manager, it intends to be a project manager. One run system. Indeed, if you think about it, Yarn lets you manage scripts. It lets you split your application into standalone models. But as we will see later, it can also be used to manage your releases cycles, to monitor how your team uses your scripts, or even to enforce standards across your monolith. All these tasks go far beyond the typical package manager, and every release pushes the boundaries further by introducing new features.

So, project managers, that sounds nice. How do we get there? What are the things that we look for when managing PRs? We're going to discuss core values. The first thing to realize is that we're a community of contributors. Open source is a very taxing environment, and most projects are struggling finding ways to make their work sustainable, and Yarn isn't exempt. To help with that, we rely a lot on our contributors to be the change that they want to see to contribute back to the project they like. In this way, we don't really see Yarn as a product. We really see it as a project. In practice, it means that our core team spends as much time working on our infrastructure as on the product itself. Recently, we moved from Webpack to ESbuild in order to make building Yarn easier. Multiple comments let you build part of the Yarn binaries from source, so you can easily try out independent features. Yarn is really all about making it possible for you to experiment well past what we as a team could offer by ourselves.

A second important value we are always keeping in mind when merging PRs is soundness. Yarn must tell you if something is wrong in your application. It shouldn't let you make mistakes. It shouldn't let them go unnoticed. It must not make uncontrolled assumptions. This might sound rigid because you have more errors than you used to, but it's really critical whether you author applications or libraries, you need to have confidence that something that works now will also work on your production or when installed by your consumers.

2. Yarn's Impact and Workflows

Short description:

Yarn helps users understand the tool and guides them along the JavaScript DevOps path. Default behaviors and user experience are crucial. Yarn's impact on the work we've made so far has been positive. Workflows like Ready Cycles and Release Workflow have improved scalability and release management. Zero installs keep the project cache within the repository.

Another one is good practices. The JavaScript landscape is large, changes fast and has many very opinionated people. As a package manager we are in a unique position to help our users understand the tool that they are using and to guide them along the hard path. Not only using Yarn should solve a practical need, it should also contribute making you learn and becoming a better engineer along the way in terms of JavaScript DevOps.

We reached the last one of this set, default behaviors, which directly tie into developer experience. Most of our users will only ever use the default commands of our tool, which is a good thing because most of them, they don't have to remember a bunch of command line flags just for the sake of running one specific behavior. For instance, the fact that you can run any script by prefixing it in your CLI with just Yarn, may look a very simple thing, but it surely is one of the reasons some people might find it appealing. User experience is really important and crucial to the Yarn user experience.

We've now seen a bit of what Yarn claims to be. Now, we are going to talk a bit about DevOps. What does Yarn actually do for you, practically speaking? We are going to go over two interesting stories of projects who adopted it. One is an open source project and the other is an internal application that we use at my company. Without surprise, the first one is Yarn itself. Before we dive in, let me tell you a funny story. Back in Yarn 1, we didn't actually use Workspaces to develop Yarn. It was a real problem for us because not only were problems hidden away from the maintainers themselves, we weren't also directly confronted with the values that some features might have. As a result, we were not emerging things even though perhaps we should have just because we couldn't see how impactful they would actually be. When you use Workspaces, it's very apparent that, for example, you need to be able to run a script in all your Workspaces at once. But since we didn't use the Workspaces, it didn't really seem like a huge deal for us at the time. Nowadays, we have an informal rule that the Yarn team needs to use all features shipped into the core, and I believe that had a strong positive impact on the work we've made so far by forcing us to use all the features that we ship as part of Yarn itself.

So, now let's talk about Workflows. The first one we are going to discuss are Ready Cycles. Our previous process back in Yarn 1 was very simple. We just had a single file at the root of the repository, and each PR that people were making was expected to add one line to it. It worked fine, but after switching to a monorepo, it wouldn't have scaled very well since we needed the ability to release each workspace, each package, by itself. So, we developed the Release Workflow, which is not unlike the chance set package that you may know. The idea is that each PR we merge also has to include a little file created by Yarn itself that lists all the workspaces that have been changed by the PR and whether they need to be part of the next release. Our CI validates these files content, and at release time, we simply instruct Yarn to aggregate all version files into classic package bums. Another workflow that we are using are zero installs. The idea is that we decided to keep the cache of the project inside the repository itself.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

pnpm – a Fast, Disk Space Efficient Package Manager for JavaScript
DevOps.js Conf 2022DevOps.js Conf 2022
31 min
pnpm – a Fast, Disk Space Efficient Package Manager for JavaScript
Watch video: pnpm – a Fast, Disk Space Efficient Package Manager for JavaScript
pnpm is a fast and efficient package manager that gained popularity in 2021 and is used by big tech companies like Microsoft and TikTok. It has a unique isolated node module structure that prevents package conflicts and ensures each project only has access to its own dependencies. pnpm also offers superior monorepo support with its node module structure. It solves the disk space usage issue by using a content addressable storage, reducing disk space consumption. pnpm is incredibly fast due to its installation process and deterministic node module structure. It also allows file linking using hardlinks instead of symlinks.
The Zen of Yarn
DevOps.js Conf 2022DevOps.js Conf 2022
31 min
The Zen of Yarn
Let's talk about React and TypeScript, Yarn's philosophy and long-term relevance, stability and error handling in Yarn, Yarn's behavior and open source sustainability, investing in maintenance and future contributors, contributing to the JavaScript ecosystem, open-source contribution experience, maintaining naming consistency in large projects, version consistency and strictness in Yarn, and Yarn 4 experiments for performance improvement.
Yarn 4 - Modern Package Management
JSNation 2022JSNation 2022
28 min
Yarn 4 - Modern Package Management
Top Content
Yarn is a package manager that focuses on stability, performance, and security. It offers unique features like plug and play installation, support for nonmodules, and the exec protocol. Yarn is committed to being a good citizen in the open-source community and contributes to fixing dependencies. It is part of the Node.js Loader's working group and advocates for Corepack. Yarn is still experimental but is improving its user experience and security features. Contributions are welcome, and switching to Yarn can improve performance in large projects.
Understanding Package Resolution in Node.js
Node Congress 2024Node Congress 2024
11 min
Understanding Package Resolution in Node.js
In this Talk, the speaker discusses package resolution in Node.js, covering topics such as CommonJS, ES modules, package.json structure, and package.json loader. The Talk also touches on conditional loading and file extension resolution, module import and export, module type determination based on file extensions and package.json, module resolution strategies in Node.js, and tips for improving loading time in ESM applications.
Security Controls in the JavaScript Supply Chain
JSNation 2022JSNation 2022
28 min
Security Controls in the JavaScript Supply Chain
This talk discusses the security challenges in the JavaScript ecosystem, including supply chain security, lock file tampering, and arbitrary command execution. It highlights the risks of blind upgrades and hidden comments in code. The talk also covers dependency confusion attacks and the importance of establishing a threat model for node applications.
Taming the State Management Dragon
React Summit US 2023React Summit US 2023
23 min
Taming the State Management Dragon
Watch video: Taming the State Management Dragon
This Talk discusses various aspects of state management in software development. It covers different types of state, such as bootstrap data, lazily loaded data, and reactive data. The Talk also explores the concept of locality in state management, including local, global, and regional state. It introduces libraries like Recoil and Jotai that challenge the single global store concept and provide better locality. The Talk emphasizes the importance of setting up state management systems for success and creating reliable systems to focus on user satisfaction.