Skip to main content

Side by side

npmvsYarn

What is the difference between npm and Yarn?

Updated 3 min read8 differences

In short

npm ships with Node.js and installs into node_modules; Yarn is a separate client for the same registry, with its own lockfile and Plug'n'Play installs.

npm

Node Package Manager

npm is Node.js's default package manager and the world's largest software registry; it downloads a project's dependencies and tracks their versions.

Read the page on npm

Yarn

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.

Read the page on Yarn

npm and Yarn compared

AspectnpmYarn
OriginCreated in 2010; ships with Node.jsReleased by Facebook in 2016; installed separately
Lockfilepackage-lock.jsonyarn.lock
Package layoutA node_modules folder with hoisted packagesPlug'n'Play by default in modern Yarn; node_modules optional
Running scriptsnpm run build; only a few, such as npm test, skip runyarn build; the word run is optional
One-off commandsnpx create-viteyarn dlx create-vite
MonoreposWorkspaces since 2020, with --workspace flagsWorkspaces since 2017; modern Yarn adds yarn workspaces foreach
SetupNothing extra to installPinned in packageManager, set up with Corepack
Best forThe default path with no extra toolingTeams that want Plug'n'Play, zero-installs or Yarn's monorepo tools

The difference, explained

npm and Yarn are both package managers for JavaScript: they read a project's package.json, download its dependencies from the same npm registry and lock their exact versions. npm is the default, created in 2010 and shipped with every installation of Node.js. Yarn was released by Facebook in 2016 as a faster, more predictable alternative, so it is a different client for the same packages, not a separate ecosystem.

Yarn's early lead came from ideas npm later adopted: a lockfile, yarn.lock, that pins every version, an offline cache and workspaces for several packages in one repository. npm added package-lock.json in 2017 and workspaces in 2020, so npm and Yarn Classic (version 1) now work much alike. The bigger difference today is modern Yarn, version 2 and later: its default Plug'n'Play mode skips node_modules and writes a single .pnp.cjs file that tells Node.js where each package sits in Yarn's cache, which makes installs lighter and makes undeclared imports fail.

Day to day, the commands are close: yarn add react for npm install react, and yarn build for npm run build, since Yarn doesn't need the word run. npm needs no setup, while modern Yarn is pinned per project in the packageManager field of package.json and usually set up with Corepack, which shipped with Node.js until version 25 and is now installed with npm install -g corepack. Tools that expect a real node_modules folder keep working under Yarn with nodeLinker: node-modules in .yarnrc.yml.

A common mistake is to mix the two in one project. Each writes its own lockfile, so installs drift apart; pick one, delete the other lockfile and record the choice in packageManager. Another misconception is that Yarn is always much faster: npm has closed much of the early gap, so the choice today is mostly about workflow, install layout and the monorepo tools a team prefers.

Which one should you use?

Choose npm when…

  • You want the default that ships with Node.js and needs no setup.
  • Your hosting, CI images or tutorials assume npm install and package-lock.json.
  • Your tools expect a regular node_modules folder.

Choose Yarn when…

  • You want Plug'n'Play's strict dependency checks and lighter installs.
  • You manage a monorepo and want commands such as yarn workspaces foreach.
  • You want zero-installs: committing Yarn's cache so a fresh clone runs without installing.
  • The project already uses Yarn and its yarn.lock.

The same everyday tasks in npm and Yarn

npmbash
# npm: comes with Node.js
npm install                # install from package-lock.json
npm install react          # add a dependency
npm install -D vitest      # add a dev dependency
npm uninstall lodash       # remove a dependency
npm run build              # run a package.json script
npx create-vite            # run a package once
Yarnbash
# Yarn: pinned in package.json's packageManager field
yarn                       # install from yarn.lock
yarn add react             # add a dependency
yarn add -D vitest         # add a dev dependency
yarn remove lodash         # remove a dependency
yarn build                 # run a script, no "run" needed
yarn dlx create-vite       # run a package once

Readers ask

Is Yarn faster than npm?

It was clearly faster when it appeared in 2016, and npm has closed much of that gap since. Modern Yarn with Plug'n'Play can still install large projects quicker because it writes far fewer files, but results depend on the project and the cache.

Should I use Yarn Classic or modern Yarn?

For new projects, modern Yarn: Yarn Classic (version 1) only gets occasional fixes. If Plug'n'Play causes problems with a tool, set nodeLinker: node-modules and keep the rest of modern Yarn.

How do I switch a project from npm to Yarn?

Set up Yarn with Corepack, delete package-lock.json and node_modules, then run yarn to resolve the dependencies from package.json and write yarn.lock. Commit the new lockfile and run the tests, since some versions may shift.

More

Settings