Fast, reliable, and secure dependency management tool for JavaScript/Node.js projects
Commands for managing yarn's package cache system, which stores downloaded packages for offline installation and performance optimization.
Display the location of yarn's cache directory.
yarn cache dirUsage Examples:
# Show cache directory path
yarn cache dir
# Use in scripts
CACHE_DIR=$(yarn cache dir)
echo "Cache is located at: $CACHE_DIR"
# Check cache size
du -sh $(yarn cache dir)Default Cache Locations:
~/.cache/yarn~/Library/Caches/yarn%LOCALAPPDATA%\Yarn\cacheDisplay all packages stored in the cache.
yarn cache list [pattern] [options]
# Options:
--pattern <pattern> # Filter packages by patternUsage Examples:
# List all cached packages
yarn cache list
# Filter by pattern
yarn cache list --pattern "react*"
yarn cache list --pattern "@types/*"
# List specific package versions
yarn cache list lodash
yarn cache list reactOutput Format:
react@16.14.0
react@17.0.2
react@18.2.0
react-dom@16.14.0
react-dom@17.0.2
react-dom@18.2.0Remove packages from the cache.
yarn cache clean [package] [options]
# Options:
--all # Clean entire cache
--pattern <pattern> # Clean packages matching patternUsage Examples:
# Clean specific package
yarn cache clean react
# Clean all versions of a package
yarn cache clean lodash
# Clean packages matching pattern
yarn cache clean --pattern "react*"
yarn cache clean --pattern "@babel/*"
# Clean entire cache
yarn cache clean --all
yarn cache clean
# Force clean (skip confirmation)
yarn cache clean --all --forceClean Process:
--force used)~/.cache/yarn/
├── v6/ # Cache version
│ ├── npm-react-18.2.0-<hash> # Package tarball
│ ├── npm-lodash-4.17.21-<hash>
│ └── npm-@types-node-18.7.14-<hash>
├── .tmp/ # Temporary files during download
└── .yarnclean # Auto-clean rules (if enabled)Each cached package includes:
Yarn uses versioned caches:
# Set custom cache directory
yarn config set cache-folder /custom/cache/path
# Use environment variable
export YARN_CACHE_FOLDER=/tmp/yarn-cache
# Temporary cache directory
YARN_CACHE_FOLDER=/tmp/cache yarn install# Disable cache (force download)
yarn install --no-cache
# Use cache only (offline mode)
yarn install --offline
# Clear cache before download
yarn install --force# Check cache size
du -sh $(yarn cache dir)
# Set cache size limit (not directly supported, but can script)
find $(yarn cache dir) -type f -atime +30 -delete # Remove files older than 30 daysCreate an offline mirror for package distribution:
# Set offline mirror directory
yarn config set offline-mirror ./offline-packages
# Install packages (populates offline mirror)
yarn install
# Use offline mirror
yarn install --offlineOffline Mirror Structure:
offline-packages/
├── lodash-4.17.21.tgz
├── react-18.2.0.tgz
├── react-dom-18.2.0.tgz
└── ...# Install from cache only
yarn install --offline
# Install with offline fallback
yarn install --prefer-offline
# Check if package exists in cache
yarn cache list react | grep "react@18.2.0"# Pre-populate cache for project
yarn install --cache-folder /shared/cache
# Share cache between projects
export YARN_CACHE_FOLDER=/shared/yarn-cache
cd project1 && yarn install
cd project2 && yarn install # Uses same cacheCI/CD Cache Sharing:
# Save cache
tar -czf yarn-cache.tar.gz $(yarn cache dir)
# Restore cache
tar -xzf yarn-cache.tar.gz -C $(dirname $(yarn cache dir))
# Docker cache sharing
COPY yarn-cache.tar.gz /tmp/
RUN tar -xzf /tmp/yarn-cache.tar.gz -C ~/.cache/Team Cache Sharing:
# Network cache location
yarn config set cache-folder /nfs/shared/yarn-cache
# Or use offline mirror
yarn config set offline-mirror /nfs/shared/offline-packages# Automated cache cleanup script
#!/bin/bash
CACHE_DIR=$(yarn cache dir)
OLD_SIZE=$(du -sh "$CACHE_DIR" | cut -f1)
# Remove packages older than 60 days
find "$CACHE_DIR" -type f -mtime +60 -delete
# Remove empty directories
find "$CACHE_DIR" -type d -empty -delete
NEW_SIZE=$(du -sh "$CACHE_DIR" | cut -f1)
echo "Cache cleaned: $OLD_SIZE -> $NEW_SIZE"# Cache hit rate analysis
yarn install --verbose 2>&1 | grep -E "(fetch|cache)"
# Cache size analysis
find $(yarn cache dir) -name "*.tgz" | wc -l # Number of cached packages
du -sh $(yarn cache dir) # Total cache size# Optimize network settings for cache population
yarn config set network-concurrency 16 # More concurrent downloads
yarn config set network-timeout 300000 # Longer timeout for large packages
# Optimize for SSD storage
yarn config set cache-folder /fast/ssd/yarn-cache# Symptoms: Installation failures, integrity check errors
# Solution: Clean and rebuild cache
yarn cache clean --all
yarn install --force# Check cache size
du -sh $(yarn cache dir)
# Clean old packages
yarn cache clean --pattern "*" --older-than 30d # If supported
find $(yarn cache dir) -type f -mtime +30 -delete
# Move cache to larger disk
yarn config set cache-folder /path/to/larger/disk/yarn-cache# Fix cache permissions
sudo chown -R $(whoami) $(yarn cache dir)
chmod -R 755 $(yarn cache dir)
# Use user-specific cache
yarn config set cache-folder ~/.local/share/yarn-cache# Bypass cache for network debugging
yarn install --no-cache --verbose
# Use offline mode to test cache
yarn install --offline
# Clear and repopulate cache
yarn cache clean --all
yarn install --force# GitHub Actions cache example
- name: Cache Yarn dependencies
uses: actions/cache@v3
with:
path: $(yarn cache dir)
key: yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
yarn-
# GitLab CI cache example
cache:
key: yarn-$CI_COMMIT_REF_SLUG
paths:
- $(yarn cache dir)# Multi-stage cache optimization
FROM node:18 as cache
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile
FROM node:18 as production
WORKDIR /app
COPY --from=cache /app/node_modules ./node_modules
COPY . .
RUN yarn build# Team development setup
echo "cache-folder /shared/yarn-cache" >> .yarnrc
git add .yarnrc
# Local development optimization
yarn config set cache-folder ~/.cache/yarn
yarn config set network-concurrency 8Install with Tessl CLI
npx tessl i tessl/npm-yarn