Yarn Berry Documentation

repository·master·Indexed 27 days ago

https://github.com/yarnpkg/berry

A modern, fast, and secure package manager featuring a modular architecture. Documentation covers native workspaces, Plug'n'Play (PnP), and a variety of official plugins including @yarnpkg/plugin-catalog for centralized dependency management, @yarnpkg/plugin-compat for PnP compatibility, and @yarnpkg/plugin-nm for node_modules installation strategy. It also details the use of specialized protocols such as exec:, jsr:, link:, and portal:.

Tokens
71.1K
Snippets
192
Records
559
Agent score
91%

What's inside Yarn

  1. Introduction to Yarn

    master

    Yarn is an open-source package manager for JavaScript projects designed to manage dependencies through installation, updating, configuring, and removing packages. It focuses on speed, correctness, security, and developer experience.

    Key features include:

    • Workspaces: For managing monorepos.
    • Offline Caching: To speed up subsequent installs.
    • Parallel Installs: To optimize installation speed.
    • Hardened Mode: For enhanced security.
    • Interactive Commands: To improve developer workflow.

    Note: Unlike many other package managers that defer to npm for non-install commands, Yarn reimplements all commands to ensure full control over stability and the developer experience.

  2. Overview of collected telemetry data

    master

    Yarn collects certain usage metrics to help prioritize development. Data is sent in batches approximately every seven days. Note that telemetry is never run in CI environments.

    Collected data includes:

    • The Yarn version
    • The command name used (arguments are excluded)
    • Active plugin names (only official Yarn plugins; third-party plugins are hidden)
    • Number of installs run per week
    • Number of different projects installed
    • Number of installs using the nm linker
    • Number of workspaces
    • Number of dependencies
    • packageExtensions field details (name of extended package + name of the extra dependency)
    • IP address
  3. Use `@yarnpkg/fslib` for filesystem abstraction

    master
    The @yarnpkg/fslib library provides a TypeScript abstraction over Node.js filesystem APIs. It is designed to handle type-safe path transformations, support custom filesystem layers (like zip archives via ZipFS), and provide a consistent interface through FakeFS implementations.
  4. Compatibility features provided by @yarnpkg/plugin-compat

    master

    The @yarnpkg/plugin-compat plugin applies built-in patches to packages that are not compatible with Plug'n'Play (PnP) resolution out-of-the-box. Key features include:

    • Package Extensions: Various packageExtensions are enabled by default to fix metadata issues in existing packages.
    • fsevents: Makes fsevents aware of the virtual filesystem.
    • typescript: Provides auto-merging capabilities for TypeScript.
    • resolve: Implements normalize-options.js for the resolve package.
  5. Understand Yarn Plug'n'Play (PnP)

    master

    Yarn Plug'n'Play (PnP) is the default installation strategy in modern Yarn releases. Instead of creating a node_modules folder, Yarn PnP generates a single Node.js loader file named .pnp.cjs. This file contains the complete dependency tree information, telling tools exactly where packages are located on disk and how to resolve require and import calls.

    Key Advantages:

    • Minimal install footprint: Reduces I/O by generating the loader instead of copying files.
    • Shared installs across disks: Reuses package artifacts via cache paths rather than hardlinks.
    • Perfect hoisting: Avoids the risks and duplication associated with traditional node_modules hoisting.
    • Ghost dependency protection: Prevents access to undeclared dependencies, ensuring application stability.
    • Semantic erroring: Provides detailed error messages explaining why a module resolution failed (e.g., identifying missing peer dependencies or undeclared dependencies).
  6. Understand Yarn's Modular Architecture

    master

    Yarn is built using a modular architecture that separates core logic from third-party interactions via a plugin system.

    • @yarnpkg/core: The core package containing the fundamental logic for managing a project. It exposes base components such as Configuration, Project, Workspace, Cache, and Manifest.
    • @yarnpkg/cli: An indirection layer used for command-line access. It is responsible for hydrating a project instance based on the current working directory (cwd) and injecting prebuilt Yarn plugins into the environment.
    • Plugins: Most business logic related to external interactions (e.g., the npm resolver) is externalized into plugins. This allows developers to write custom logic without modifying the Yarn codebase.