Yarn 1 (Classic) Documentation

website·Indexed 18 days ago

https://classic.yarnpkg.com/en/docs/

Documentation for Yarn 1 (Classic), a fast, reliable, and secure package manager for JavaScript. Covers dependency management, .yarnrc configuration, offline mirrors, workspaces, selective version resolutions, and CLI commands such as yarn config and yarn version.

Tokens
33K
Snippets
245
Records
299
Agent score
93%

What's inside Yarn 1 (Classic)

  1. Overview of Yarn package manager

    Yarn is a package manager that enables developers to use and share code (such as JavaScript) globally. It manages code shared as 'packages' (or 'modules'), which consist of the shared source code and a package.json file that describes the package.
  2. Overview of Yarn Workspaces

    Yarn Workspaces is a feature that allows developers to manage multiple packages (monorepos) within a single repository. It enables the installation of dependencies from multiple package.json files located in subfolders using a single root package.json. Key benefits include faster and lighter installations by preventing package duplication (hoisting) and automatic creation of symlinks between workspaces that depend on each other.
  3. Overview of the Yarn dependency workflow

    Using Yarn as a package manager introduces a specific workflow for managing project dependencies. The core stages of this workflow include:

    1. Creating a new project
    2. Adding, updating, or removing dependencies
    3. Installing or reinstalling dependencies
    4. Integrating with version control systems (e.g., Git)
    5. Configuring Continuous Integration (CI) pipelines
  4. Understand package ownership in Yarn

    A package owner is a user in the registry with permissions to modify a package. There are currently no granular roles; users either have full owner access or no access. Owners can publish new versions, change package metadata, and add or remove other owners.
  5. Understand the purpose of yarn.lock

    Yarn uses a yarn.lock file in the project root to store the exact versions of every dependency installed. This ensures consistent and reproducible installs across different machines (e.g., local development and CI servers), providing more precision than the version ranges defined in package.json.
  6. Fetch general package information using yarn info

    Use yarn info <package> to retrieve metadata about a package in a tree format. The package does not need to be installed locally to fetch its information. By default, the output is a single-quoted serialization, but you can use the --json flag to emit valid JSON lines.
    # Get general info for react
    yarn info react
    
    # Get general info for react in JSON format
    yarn info react --json
  7. Yarn Classic Core Features and Capabilities

    Yarn Classic provides several mechanisms to ensure fast and reliable dependency management:

    • Caching & Speed: Caches every downloaded package and parallelizes operations to reduce install times.
    • Security: Uses checksums to verify the integrity of every installed package before execution.
    • Determinism: Uses a concise lockfile format and a deterministic algorithm to ensure identical installs across different machines regardless of install order.
    • Offline Mode: Allows re-installation of previously installed packages without an internet connection.
    • Network Resilience: Maximizes network utilization by queuing requests to avoid waterfalls and automatically retries failed requests to prevent install failure.
    • Compatibility: Supports any package from npm.
    • Flat Mode: Resolves mismatching dependency versions to a single version to prevent duplicate packages.
  8. Understand Yarn Plug’n’Play (PnP) installation strategy

    Yarn Plug’n’Play (PnP) is an alternative installation strategy that replaces the traditional node_modules directory. Instead of relying on Node's default filesystem-based resolution (which recursively searches for node_modules folders), PnP generates a single .pnp.js file. This file acts as a map that tells Node exactly where to find each package in the dependency tree, eliminating the need for expensive I/O operations and filesystem queries during installation and runtime.

    Key benefits of PnP include:

    • Faster Installations: Removes the need to copy thousands of files into node_modules, which typically accounts for a majority of yarn install time.
    • Strict Dependency Management: Prevents 'ghost dependencies' where code works because a package happens to exist in node_modules even if it isn't listed in package.json.
    • Improved Runtime Performance: Reduces the number of stat and readdir syscalls Node must perform to resolve modules, speeding up application boot times.
    • Efficient Deduplication: Overcomes the structural limitations of node_modules that hinder perfect hoisting and package sharing across projects.
  9. Ensure dependency consistency using Yarn lockfiles

    Yarn uses a yarn.lock file to maintain consistency across different machines and environments. This file stores the exact versions of every installed dependency and their checksums. This prevents "Works On My Machine" problems and protects against accidental breaking changes in minor or patch versions of dependencies that follow Semantic Versioning (SemVer).
  10. Manage package dependencies with Yarn

    Yarn manages project dependencies using two primary files: package.json and yarn.lock.

    • package.json: Used to declare all dependencies (development, production, and optional), specifying the package name and the minimum version required.
    • yarn.lock: Ensures installation consistency across different environments by storing the exact versions of dependencies that were installed.
  11. Configure package metadata and licensing

    Use the description, keywords, and license fields to provide information about your package for users and package managers.

    License Requirements:

    • Use a valid SPDX license identifier (e.g., MIT).
    • Use SPDX license expression syntax 2.0 for multiple licenses.
    • Use SEE LICENSE IN <filename> for non-standard licenses pointing to a file in the package root.
    • Use UNLICENSED for private/unpublished packages where no rights are granted.
    {
      "description": "My short description of my awesome package",
      "keywords": ["short", "relevant", "keywords"],
      "license": "MIT"
    }
  12. Restrict package compatibility by CPU architecture

    The cpu field specifies the CPU architectures the package is compatible with. Yarn validates this against process.arch. Use an array of allowed architectures or the ! prefix to exclude them.
    {
      "cpu": ["x64", "ia32"],
      "cpu": ["!arm", "!mips"]
    }