yarn — an alternative package manager for Node.js, created by Facebook together with Exponent, Google and Tilde in 2016. yarn solved the speed and security problems of npm 2-4: parallel installation, strict lock file and offline cache. Today yarn remains popular in React Native, Expo and monorepos. Learn more at yarn Documentation.
Key Takeaways
yarn (Yet Another Resource Negotiator) was released in October 2016 as Facebook's response to the problems of npm 2-4: slow installation, lack of a lock file, and insecure packages. yarn introduced parallel package downloads, deterministic yarn.lock, and integrity verification through checksums. In 2020 yarn transitioned to version 2 (Berry) with Plug'n'Play and a TypeScript-first approach.
yarn architecture differs from npm: yarn downloads packages in parallel (npm before version 5 — sequentially), caches each package in ~/.cache/yarn without duplication between projects, and verifies integrity via checksums. yarn always generates yarn.lock with every installation, while package-lock.json in npm only appeared in version 5.
Starting with yarn 2 (Berry, 2020), yarn was completely rewritten in TypeScript and supports Plug'n'Play, workspaces, constraints for monorepos, and installation protocols from different sources (npm, git, tarball). yarn 3+ (2022) added Zero-Installs — storing the cache in the repository for instant installation without network access.
yarn install — the main command that reads package.json and yarn.lock and installs dependencies. yarn downloads packages in parallel, which speeds up installation by 2-3 times compared to npm 4 and earlier versions. yarn uses an offline cache: if a package has already been downloaded in another project, it is copied from the cache rather than downloaded again.
# Basic yarn commands
$ yarn install
$ yarn add react-native-webview
$ yarn add --dev typescript
$ yarn global add expo-cli
# Interactive update
$ yarn upgrade-interactive
# Remove duplicate packages
$ yarn dedupe
yarn add — the command to install a new package. yarn add <package> adds to dependencies, yarn add --dev — to devDependencies. yarn automatically updates yarn.lock. yarn upgrade updates packages to the latest versions within the semantic range. yarn upgrade-interactive — an interactive mode for selecting specific packages to update.
yarn cache — a global cache in ~/.cache/yarn/v6. Each package is stored once in a ZIP archive with a checksum. yarn cache is cleaned automatically, but you can run yarn cache clean manually. yarn cache dir shows the cache path. yarn cache list — lists cached packages.
yarn.lock — a lock file that records exact versions of all dependencies and their transitive packages. Unlike package-lock.json (JSON), yarn.lock uses a YAML format with a flat structure. yarn.lock is always generated automatically during yarn install or yarn add — this ensures that all developers and CI servers get an identical set of packages.
# Example yarn.lock structure
react@^18.3.0:
version: "18.3.1"
resolution: "react@npm:18.3.1"
dependencies:
loose-envify: "^1.1.0"
checksum: "abc123def456..."
react-native@^0.76.0:
version: "0.76.3"
resolution: "react-native@npm:0.76.3"
dependencies:
react: "18.3.1"
checksum: "def789ghi012..."
yarn.lock fields: version — exact package version, resolution — source and version, dependencies — its dependencies, checksum — checksum for verification. yarn.lock is committed to the repository and is the single source of truth for dependencies. If yarn.lock and package.json diverge, yarn issues a warning.
Yarn constraints — a rule system for monorepos that checks and enforces dependency versions in yarn.lock. Constraints are described in the yarn.constraints.js file using the Yarn Constraint Language. Constraints allow you to ensure that all packages in a monorepo use the same version of React or TypeScript.
React Native has traditionally been closely tied to yarn. Expo CLI, React Native CLI, and Create React Native App initially used yarn. In React Native projects, yarn.lock locks the versions of native modules, which is critically important for stable compilation via CocoaPods (iOS) and Gradle (Android). yarn workspaces are popular in React Native monorepos with shared code.
yarn workspaces in React Native monorepos: the root package.json contains "workspaces": ["packages/*", "mobile"]. yarn install creates a single node_modules folder at the root level. Packages reference each other through symlinks. npx pod-install is executed after changing native dependencies to update CocoaPods.
{
"private": true,
"workspaces": [
"packages/*",
"apps/mobile",
"apps/web"
],
"scripts": {
"mobile:start": "yarn workspace mobile start",
"mobile:ios": "yarn workspace mobile ios",
"lint": "yarn workspaces run lint"
}
}
Expo SDK 50+ officially recommends yarn for new projects created via npx create-expo-app. yarn ensures compatibility with Expo modules and automatically generates the correct yarn.lock. Zero-Installs in yarn 3+ allows committing the cache to the repository — developers get a ready-to-use environment without installing dependencies after git clone.
npm and yarn solve the same problem — managing Node.js packages. With the release of npm 5-7, the functional gap has practically disappeared. npm 7+ includes workspaces, npm ci (equivalent to yarn install --frozen-lockfile), and parallel install. However, yarn retains advantages in specific scenarios: Plug'n'Play, Zero-Installs, and a mature constraints system.
| Characteristic | npm | yarn |
|---|---|---|
| Year created | 2010 | 2016 |
| Lock file | package-lock.json (JSON) | yarn.lock (YAML) |
| Installation | Sequential (npm 5+) | Parallel |
| Plug'n'Play | No | Yes (yarn 2+) |
| Workspaces | Yes (npm 7+) | Yes |
| Zero-Installs | No | Yes (yarn 3+) |
| Cache | node_modules with flattening | Global ~/.cache/yarn |
| Implementation language | JavaScript | TypeScript (yarn 2+) |
When to choose yarn: projects on Expo SDK 50+, large monorepos (100+ packages), teams that value Zero-Installs for quick environment setup, and legacy projects already using yarn. When to choose npm: standard create-react-app projects, Next.js, Vite, teams without specific package manager requirements.
yarn 4 (2024) introduced improved performance, native ESM support, and optimization for large monorepos. npm 10+ continues to evolve with a focus on security and compatibility. The choice between npm and yarn today is a matter of team preferences and specific project requirements, not technical superiority of one tool over another.
Frequently Asked Questions
yarn initially surpassed npm in speed thanks to parallel package installation and aggressive caching. yarn.lock is automatically generated with every installation, while package-lock.json appeared in npm 5. yarn offers Plug'n'Play — an alternative to node_modules with zero installation time. With npm 7+, the difference in speed and functionality for basic scenarios is minimal.
yarn workspaces — a mechanism for managing multiple packages in a single repository. The root package.json contains the field "workspaces": ["packages/*"]. yarn install installs shared dependencies at the top level in a single node_modules, creates symlinks between local packages, and avoids duplication. Commands are run for all workspaces via yarn workspaces run <script>.
Plug'n'Play (PnP) — an alternative to the traditional node_modules folder, introduced in yarn 2. Instead of copying files, yarn creates a dependency map in .pnp.cjs and stores packages in ZIP archives in a global cache. PnP advantages: installation in seconds, Node.js startup time reduced by 30-50%, no node_modules and associated path issues.
Install yarn globally: npm install -g yarn. In the project root, run yarn import — yarn reads package-lock.json and creates an equivalent yarn.lock. Delete node_modules and package-lock.json, run yarn install to verify. Update the CI/CD configuration by replacing npm with yarn. yarn is fully compatible with package.json.
The "Integrity check failed" error occurs when yarn.lock and node_modules are out of sync. Solution: delete node_modules, yarn.lock and clear the cache (yarn cache clean), then run yarn install again. For version conflicts, use yarn upgrade-interactive to select specific updates. yarn dedupe will remove duplicate packages.
Summary
We will develop a mobile application turnkey
IT Sectr creates iOS and Android applications for startups and businesses since 2017. We will advise you and propose the best solution.
Read also