npm provides various caching mechanisms to speed up package installation and reduce network requests. Understanding the caching mechanism is crucial for optimizing build performance and troubleshooting installation issues.
npm Cache Location
Default Cache Location
npm cache is stored in the following locations by default:
- macOS/Linux:
~/.npm - Windows:
%AppData%/npm-cache
View cache location:
bashnpm config get cache
Cache Directory Structure
shell~/.npm/ ├── _cacache/ │ ├── content-v2/ │ │ └── sha512/ │ │ └── ... │ └── index-v5/ │ └── ... └── _logs/
Cache Management Commands
View Cache Information
bash# View cache size npm cache verify # View cache directory npm config get cache
Clean Cache
bash# Clean cache (npm 5+) npm cache clean --force # Clean specific package cache npm cache clean <package-name>
Verify Cache
bash# Verify cache integrity npm cache verify
How Caching Works
1. Package Download Cache
When installing packages, npm will:
- Check if the package exists in local cache
- If it exists and hasn't expired, use the cache directly
- If it doesn't exist or has expired, download from registry
- Store the downloaded package in cache
2. Cache Keys
Cache uses the SHA-512 hash of content as the key:
shell~/.npm/_cacache/content-v2/sha512/<hash>
3. Cache Index
Index files store package metadata and location information:
shell~/.npm/_cacache/index-v5/
Cache Configuration
Configure Cache Size
bash# Set cache size limit (default is 10GB) npm config set cache-max 10737418240 # Set cache minimum retention time (seconds) npm config set cache-min 3600
Configure Cache Strategy
bash# Disable cache (not recommended) npm config set cache false # Set cache directory npm config set cache /path/to/cache
Offline Installation
Use cache for offline installation:
bash# Install using cache (no network check) npm install --offline # Use cache but allow network fallback npm install --prefer-offline # Prefer network, cache as fallback npm install --prefer-online
Cache Optimization in CI/CD
GitHub Actions Example
yamlname: Build and Test on: [push, pull_request] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: '18' cache: 'npm' - name: Install dependencies run: npm ci - name: Run tests run: npm test
Docker Optimization
dockerfileFROM node:18-alpine # Copy package files COPY package*.json ./ # Install dependencies (utilize cache) RUN npm ci --only=production # Copy application code COPY . . CMD ["npm", "start"]
Custom Cache Directory
bash# Set custom cache directory export npm_config_cache=/custom/cache/path npm install
Cache-Related Issues and Solutions
1. Cache Corruption
Symptoms: Installation fails or strange errors occur
Solution:
bashnpm cache clean --force npm install
2. Cache Taking Up Too Much Space
Solution:
bash# View cache size npm cache verify # Clean cache npm cache clean --force # Regular cleanup (add to crontab) 0 0 * * 0 npm cache clean --force
3. Cache Causing Version Inconsistency
Solution:
bash# Force re-download npm install --force # Delete node_modules and package-lock.json rm -rf node_modules package-lock.json npm install
Performance Optimization Tips
1. Use npm ci
npm ci is specifically designed for CI environments and is faster than npm install:
bashnpm ci
Advantages:
- Uses package-lock.json directly
- Skips some user-level configurations
- Deletes node_modules and reinstalls
- Faster installation speed
2. Parallel Installation
npm 7+ supports parallel installation:
bash# Set parallel installation count npm config set maxsockets 50
3. Use Mirrors
Use domestic mirrors to speed up downloads:
bash# Use Taobao mirror npm config set registry https://registry.npmmirror.com # Use cnpm npm install -g cnpm --registry=https://registry.npmmirror.com
4. Pre-install Common Packages
bash# Globally install common packages npm install -g nodemon typescript # Use npm link to share local packages npm link ../local-package
Cache Monitoring
View Cache Statistics
bash# View cache size du -sh ~/.npm # View cache directory structure tree ~/.npm/_cacache -L 2
Cache Analysis Tools
bash# Use npm-cache-stats npm install -g npm-cache-stats npm-cache-stats
Best Practices
1. Development Environment
- Keep default cache configuration
- Regularly clean cache (once a month)
- Use
npm cache verifyto check cache integrity
2. CI/CD Environment
- Use
npm ciinstead ofnpm install - Configure cache layers (GitHub Actions cache, Docker volume)
- Set reasonable cache retention policies
3. Production Environment
- Use
npm ci --productionto install only production dependencies - Consider using
.npmrcto lock configuration - Regularly clean cache to free up space
4. Offline Environment
- Pre-download all dependencies to cache
- Use
npm install --offlinefor offline installation - Consider using private registry
.npmrc Configuration Example
ini# Project-level .npmrc cache=/path/to/project/.npm-cache registry=https://registry.npmmirror.com prefer-offline=true fetch-retries=3 fetch-retry-mintimeout=20000 fetch-retry-maxtimeout=120000
Common Issue Troubleshooting
Slow Installation Speed
bash# Check network connection ping registry.npmjs.org # Use mirror npm config set registry https://registry.npmmirror.com # Clean cache npm cache clean --force
Installation Failure Due to Cache Issues
bash# Clean cache npm cache clean --force # Delete node_modules and lock file rm -rf node_modules package-lock.json # Reinstall npm install
Insufficient Disk Space
bash# View cache size du -sh ~/.npm # Clean cache npm cache clean --force # Set cache size limit npm config set cache-max 5368709120
Understanding npm caching mechanisms helps developers optimize build performance, troubleshoot installation issues, and improve efficiency in CI/CD environments.