Skip to main content

Yarn

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/yarn

In short

Yarn is a JavaScript package manager, an alternative to npm, that installs dependencies from the npm registry and adds a lockfile, workspaces and Plug'n'Play.

What is Yarn?

Yarn was created at Facebook and released in 2016, when npm installs were slow and could give different results on different machines. It reads the same package.json and downloads packages from the same npm registry, but it introduced ideas that npm later adopted: a lockfile, yarn.lock, that pins the exact version of every package, and workspaces for managing several packages in one repository. It also keeps downloaded packages in a cache, so repeat installs can work offline.

Yarn has two lines today. Yarn 1, now called Yarn Classic, works much like npm, with a node_modules folder, and gets only occasional fixes. Modern Yarn, version 2 and later and nicknamed Berry, is a rewrite whose default install mode is Plug'n'Play (PnP): instead of copying thousands of files into node_modules, it writes a single .pnp.cjs file that tells Node.js where each package sits in Yarn's cache. Projects whose tools expect a real node_modules folder can set nodeLinker: node-modules in .yarnrc.yml.

Everyday commands differ slightly from npm's: yarn alone installs everything, yarn add react adds a dependency, and yarn test runs a script without the word run. Modern Yarn is pinned per project in the packageManager field of package.json and is usually set up with Corepack, a small helper that shipped with Node.js until version 25 and is now installed with npm install -g corepack.

A common confusion is to see Yarn as a separate ecosystem. It isn't: packages come from the same registry, and a library published with npm installs fine with Yarn. What differs is the client: its commands, its lockfile format, how it lays out packages and how fast it installs. npm has since caught up on lockfiles and workspaces, so the choice is mostly about workflow, and a project should stick to one tool and commit only its lockfile.

Key takeaways

  • Yarn is a JavaScript package manager that uses the same npm registry and package.json.
  • It popularized lockfiles (yarn.lock) and workspaces, which npm later adopted.
  • Yarn Classic (version 1) uses node_modules and only gets occasional fixes.
  • Modern Yarn defaults to Plug'n'Play, which replaces node_modules with one .pnp.cjs file.
  • Pick one package manager per project and commit only its lockfile.

Example

Yarn commands next to their npm equivalentsbash
yarn                   # install everything from yarn.lock   (npm install)
yarn add react         # add a dependency                     (npm install react)
yarn add -D vitest     # add a dev dependency                 (npm install -D vitest)
yarn remove lodash     # remove a dependency                  (npm uninstall lodash)
yarn test              # run a script from package.json       (npm run test)
yarn dlx create-vite   # run a package once without adding it (npx create-vite)

Readers ask

What is the difference between Yarn and npm?

Both install packages from the npm registry using package.json. They differ in their commands, their lockfiles (yarn.lock versus package-lock.json) and how they lay out packages: modern Yarn's Plug'n'Play skips the node_modules folder entirely.

What is the difference between Yarn Classic and Yarn Berry?

Yarn Classic is version 1, which works much like npm and is no longer actively developed. Berry is the name for modern Yarn, version 2 and later: a rewrite with Plug'n'Play, plugins and stricter dependency rules.

Can I use Yarn and npm in the same project?

It works, but it is a bad idea: each writes its own lockfile, so installs can drift apart. Choose one, delete the other lockfile and record the choice in the packageManager field.

See also

Sources

Spotted a mistake or something missing on this page?Suggest an edit

More

Settings