Skip to content
Latchkey

What Is Yarn? The JavaScript Package Manager Explained

Yarn is a JavaScript package manager that popularized lockfiles and offers fast, deterministic installs and first-class monorepo workspaces.

Yarn was created to fix early npm pain points around speed and determinism, and it introduced the lockfile concept that is now standard everywhere. It reads the same package.json as npm and comes in two major lines: the original "Classic" (1.x) and the rewritten "Berry" (2.x and later).

What Yarn is

Yarn is a command-line package manager for Node.js projects. It resolves dependencies declared in package.json, records exact versions in yarn.lock, and supports workspaces for managing many packages in one repository. Classic Yarn uses a conventional node_modules; Berry defaults to Plug and Play.

Classic vs Berry and Plug and Play

Yarn Classic (1.x) is still widely used and behaves much like npm. Yarn Berry (2+) introduced Plug and Play (PnP), which skips node_modules entirely and resolves modules from a single .pnp.cjs map, plus zero-installs where the cache is committed to the repo. PnP is faster and stricter but occasionally trips up tools that assume node_modules exists.

A usage example

The everyday commands are short and familiar.

Common Yarn commands
# install exactly from the lockfile (use in CI)
yarn install --immutable

# add a dependency
yarn add axios

# run a script
yarn build

Role in CI/CD

In pipelines, "yarn install --immutable" (Berry) or "yarn install --frozen-lockfile" (Classic) guarantees the lockfile is respected and fails if it would change. Caching the Yarn cache directory speeds up repeat installs. With Berry zero-installs, the dependency cache lives in the repo, so CI can skip the install network step entirely.

Alternatives

npm is the default and requires no extra install. pnpm offers the best disk efficiency and install speed via its shared store. Yarn remains a strong choice for teams that value its mature workspaces, PnP strictness, or zero-install workflow.

Key takeaways

  • Yarn popularized lockfiles and offers deterministic, fast installs.
  • Berry adds Plug and Play and zero-installs; Classic stays node_modules-based.
  • Use "yarn install --immutable" in CI to enforce the lockfile.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →