๐Ÿ”ง DevOps

Yarn

Yarn

A JavaScript package manager. An npm alternative. Fast installation, caching.

Detailed Explanation

Yarn is a JavaScript package manager developed in 2016 at Facebook (now Meta). It was created to address npm's speed and security issues, and it guarantees consistent installation through deterministic dependencies and a lock file.

Its key features include fast speed through parallel installation, offline caching, precise version management through yarn.lock, and monorepo support through the Workspaces feature. Starting with Yarn 2 (Berry), dependencies can be managed without node_modules using Plug'n'Play (PnP) mode.

When installing packages, it uses a global cache to avoid downloading the same package more than once. It analyzes each project's dependency tree and installs it in an optimized structure, and it guarantees package integrity through checksum verification.

In practice, CI/CD pipelines use yarn --frozen-lockfile to install without changing the lock file, and in monorepo environments, multiple packages are managed efficiently with workspaces. It is 99% compatible with npm, which makes migration easy.

Code Example

# ํ”„๋กœ์ ํŠธ ์ดˆ๊ธฐํ™”
yarn init -y

# ํŒจํ‚ค์ง€ ์„ค์น˜ (dependencies)
yarn add react react-dom

# ๊ฐœ๋ฐœ ์˜์กด์„ฑ์œผ๋กœ ์„ค์น˜
yarn add -D typescript @types/react

# ๊ธ€๋กœ๋ฒŒ ์„ค์น˜
yarn global add create-react-app

# ๋ชจ๋“  ์˜์กด์„ฑ ์„ค์น˜ (CI ํ™˜๊ฒฝ)
yarn install --frozen-lockfile

# ํŒจํ‚ค์ง€ ์—…๋ฐ์ดํŠธ
yarn upgrade react@latest

# ์›Œํฌ์ŠคํŽ˜์ด์Šค ์„ค์ • (package.json)
# {
#   "private": true,
#   "workspaces": ["packages/*"]
# }

# ํŠน์ • ์›Œํฌ์ŠคํŽ˜์ด์Šค์—์„œ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰
yarn workspace @myapp/web dev

# ์บ์‹œ ์ •๋ฆฌ
yarn cache clean

# ์˜์กด์„ฑ ์ด์œ  ํ™•์ธ
yarn why lodash

How It's Said in Practice

Senior: "Package installation is slow in CI โ€” have you checked the yarn cache settings?"

Junior: "Ah, after I added a cache action in GitHub Actions, the installation time dropped from 3 minutes to 40 seconds."

Interviewer: "What are the differences between npm and Yarn, and is there a reason you choose Yarn?"

Candidate: "Yarn is faster thanks to parallel installation, and the deterministic nature of yarn.lock is beneficial for team collaboration. I especially prefer it because the Workspaces feature makes monorepo management convenient."

Take a mock interview on this topic โ†’ WIA SOOM AI Interview ยท Start for free

Reviewer: "There's a change to the yarn.lock file โ€” was that intentional?"

Developer: "It changed along with the react version update. The change details are in the yarn upgrade log."

Cautions

Learn More