CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-yarn

Fast, reliable, and secure dependency management tool for JavaScript/Node.js projects

Overview
Eval results
Files

cache-management.mddocs/

Cache Management

Commands for managing yarn's package cache system, which stores downloaded packages for offline installation and performance optimization.

Capabilities

Show Cache Directory

Display the location of yarn's cache directory.

yarn cache dir

Usage 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:

  • Linux: ~/.cache/yarn
  • macOS: ~/Library/Caches/yarn
  • Windows: %LOCALAPPDATA%\Yarn\cache

List Cached Packages

Display all packages stored in the cache.

yarn cache list [pattern] [options]

# Options:
--pattern <pattern>      # Filter packages by pattern

Usage 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 react

Output 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.0

Clean Cache

Remove packages from the cache.

yarn cache clean [package] [options]

# Options:
--all                    # Clean entire cache
--pattern <pattern>      # Clean packages matching pattern

Usage 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 --force

Clean Process:

  1. Identifies packages to remove
  2. Shows confirmation prompt (unless --force used)
  3. Removes package tarballs and metadata
  4. Updates cache index

Cache Structure

Cache Organization

~/.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)

Cache Metadata

Each cached package includes:

  • Tarball: Compressed package contents
  • Metadata: Package.json and registry information
  • Integrity hash: For verification
  • Download timestamp: For cache management

Cache Versioning

Yarn uses versioned caches:

  • v4: Yarn Classic (1.x) cache format
  • v6: Current cache format
  • Migration: Automatic when upgrading Yarn versions

Cache Configuration

Cache Directory

# 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

Cache Behavior

# Disable cache (force download)
yarn install --no-cache

# Use cache only (offline mode)
yarn install --offline

# Clear cache before download
yarn install --force

Cache Size Management

# 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 days

Offline Mode

Offline Mirror

Create 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 --offline

Offline Mirror Structure:

offline-packages/
├── lodash-4.17.21.tgz
├── react-18.2.0.tgz
├── react-dom-18.2.0.tgz
└── ...

Offline Installation

# 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"

Cache Optimization

Preloading Cache

# 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 cache

Cache Sharing

CI/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

Cache Maintenance

# 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 Performance

Cache Benefits

  1. Faster Installs: Packages downloaded once, reused everywhere
  2. Offline Capability: Install without internet connection
  3. Bandwidth Savings: Avoid redundant downloads
  4. Reliability: Local copies immune to network issues

Cache Statistics

# 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

Performance Tuning

# 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

Troubleshooting Cache Issues

Cache Corruption

# Symptoms: Installation failures, integrity check errors
# Solution: Clean and rebuild cache
yarn cache clean --all
yarn install --force

Disk Space Issues

# 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

Permission Issues

# 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

Network Issues

# 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

Cache Integration

CI/CD Integration

# 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)

Docker Integration

# 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

Development Workflow

# 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 8

Install with Tessl CLI

npx tessl i tessl/npm-yarn

docs

cache-management.md

configuration.md

index.md

information-commands.md

package-management.md

project-management.md

registry-operations.md

utility-commands.md

workspace-management.md

tile.json