Yarn

Gestor de paquetes JavaScript rapido - workspaces, cache y Plug'n'Play para gestion eficiente de dependencias

TL;DR

Qué: Gestor de paquetes JavaScript rápido, confiable y seguro.

Por qué: Instalaciones determinísticas, workspaces, plug’n’play, caché offline.

Quick Start

Instalar:

# npm
npm install -g yarn

# Homebrew
brew install yarn

# Check version
yarn --version

Uso básico:

yarn init
yarn add express
yarn install
yarn dev

Cheatsheet

ComandoDescripción
yarn add pkgAgregar dependencia
yarn add -D pkgAgregar dep de dev
yarn remove pkgEliminar paquete
yarn installInstalar todas las deps
yarn upgradeActualizar paquetes
yarn run scriptEjecutar script
yarn dlx pkgEjecutar paquete

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