LinuxCommandLibrary

yarn

Manage and install JavaScript package dependencies

TLDR

Install a module globally

$ yarn global add [module_name]
copy

Install all dependencies referenced in the package.json file (the install is optional)
$ yarn install
copy

Install a module and save it as a dependency to the package.json file (add --dev to save as a dev dependency)
$ yarn add [module_name]@[version]
copy

Uninstall a module and remove it from the package.json file
$ yarn remove [module_name]
copy

Interactively create a package.json file
$ yarn init
copy

Identify whether a module is a dependency and list other modules that depend upon it
$ yarn why [module_name]
copy

SYNOPSIS

yarn <command> [<options>]
yarn [<options>] (for default install action)

PARAMETERS

install
    Installs all dependencies defined in the package.json file.

add <package>...
    Adds one or more packages to the project dependencies.

remove <package>...
    Removes one or more packages from the project dependencies.

upgrade [<package>...]
    Upgrades packages to their latest compatible version based on version ranges.

run <script> [<args>...]
    Executes a script defined in the scripts section of package.json.

init
    Initializes a new Yarn project, creating a package.json file.

publish [<tarball>|
    Publishes a package to a registry.

create <starter-kit-package> [<args>...]
    Creates a new project from a starter kit or template.

config
    Manages Yarn configuration settings.

set version <version>
    Sets the Yarn version for the current project, useful for project-specific Yarn versions.

DESCRIPTION

Yarn is a fast, reliable, and secure dependency management tool for JavaScript. It acts as a package manager, similar to npm, designed to help developers manage their project's dependencies efficiently.

Yarn caches downloaded packages, parallelizes operations, and uses checksums to ensure package integrity, leading to significantly faster and more reliable installations. It works with the npm registry and supports features like workspaces for monorepos, Plug'n'Play (PnP) for optimized installations, and deterministic yarn.lock files to ensure consistent builds across different environments. Yarn aims to provide a robust and predictable workflow for front-end and back-end JavaScript development, streamlining the process of adding, removing, updating, and running project scripts.

CAVEATS

Requires Node.js to be installed on the system.
While compatible with the npm registry, its lockfile format (yarn.lock) is different from npm's (package-lock.json), leading to potential conflicts if both package managers are used interchangeably on the same project without caution.
Yarn has evolved through several major versions (Yarn Classic, Yarn Berry/Modern) with significant differences in how it operates (e.g., PnP, zero-install, workspace support via yarn dlx). Users should be aware of the specific version they are using.

WORKSPACES

Yarn workspaces allow developers to manage multiple packages within a single repository (monorepo), simplifying dependency management and cross-package linking. This feature is crucial for large projects composed of several interconnected modules.

YARN.LOCK

This file is automatically generated and ensures deterministic installations by recording the exact versions and dependencies of every package installed in a project. It guarantees that anyone running yarn install will get the identical dependency tree.

PLUG'N'PLAY (PNP)

Introduced in Yarn 2 (Berry), PnP is an experimental installation strategy that eliminates the node_modules folder. Instead, it generates a single .pnp.cjs file that maps module names to their locations, which can lead to faster startup times and more efficient disk usage.

HISTORY

Yarn was developed by Facebook (now Meta) and released in October 2016. Its primary motivation was to address perceived shortcomings in npm at the time, particularly regarding performance, security, and determinism. It quickly gained popularity due to its speed, reliable installations through lockfiles and checksums, and improved offline mode.

Over the years, Yarn has continued to evolve, with the "Yarn Berry" (v2 and above) series introducing advanced features like Plug'n'Play (PnP), yarn dlx, and enhanced monorepo support, aiming to further optimize dependency management and reduce node_modules footprint.

SEE ALSO

npm(1), pnpm(1), node(1)

Copied to clipboard