TL;DR
Was: Schneller, zuverlässiger und sicherer JavaScript-Paketmanager.
Warum: Deterministische Installationen, Workspaces, Plug’n’Play, Offline-Cache.
Quick Start
Installieren:
# npm
npm install -g yarn
# Homebrew
brew install yarn
# Check version
yarn --version
Grundlegende Verwendung:
yarn init
yarn add express
yarn install
yarn dev
Cheatsheet
| Befehl | Beschreibung |
|---|---|
yarn add pkg | Abhängigkeit hinzufügen |
yarn add -D pkg | Dev-Abhängigkeit hinzufügen |
yarn remove pkg | Paket entfernen |
yarn install | Alle Deps installieren |
yarn upgrade | Pakete aktualisieren |
yarn run script | Skript ausführen |
yarn dlx pkg | Paket ausführen |
Gotchas
Installing packages
# Production dependency
yarn add express
# Dev dependency
yarn add -D typescript
# Global package (Yarn 1)
yarn global add nodemon
# Specific version
yarn add [email protected]
# From git
yarn add git+https://github.com/user/repo.git
Workspaces
// package.json
{
"private": true,
"workspaces": [
"packages/*"
]
}
# Install all workspace deps
yarn install
# Run script in specific workspace
yarn workspace @myorg/web dev
# Add dep to workspace
yarn workspace @myorg/utils add lodash
# Run in all workspaces
yarn workspaces foreach run build
Yarn Berry (v2+)
# Enable Yarn Berry
yarn set version berry
# Enable Plug'n'Play
yarn config set nodeLinker pnp
# Or use node_modules
yarn config set nodeLinker node-modules
# Install
yarn install
Configuration (.yarnrc.yml)
# Yarn Berry config
nodeLinker: node-modules
# Registry
npmRegistryServer: "https://registry.npmmirror.com"
# Plugins
plugins:
- path: .yarn/plugins/@yarnpkg/plugin-workspace-tools.cjs
spec: "@yarnpkg/plugin-workspace-tools"
Scripts
{
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"test": "vitest"
}
}
# Run script
yarn dev
yarn run build
# Execute binary
yarn exec vitest
# Execute without installing
yarn dlx create-react-app my-app
Useful commands
# Check why package is installed
yarn why lodash
# Upgrade interactive
yarn upgrade-interactive
# Check outdated
yarn outdated
# Clean cache
yarn cache clean
# List packages
yarn list --depth=0
Next Steps
- Yarn Documentation - Offizielle Dokumentation
- Yarn Berry - v2+ Anleitung
- Workspaces - Monorepo
- Plug’n’Play - Zero-Install