npm cache clean 후 설치 속도 개선
Optimizing npm installation speed by managing the cache, using ci instead of install, and configuring registry mirrors.
npm cache clean 후 설치 속도 개선
Introduction
Slow npm install times can waste hours of development time. After cleaning the cache, installations sometimes become even slower because packages must be re-downloaded. This post covers how to properly manage npm cache for optimal installation speed.
Environment
node --version
# v20.11.0
npm --version
# 10.2.4
# Check current cache size
npm cache ls 2>/dev/null | wc -l
# Or on Windows:
dir /s %LOCALAPPDATA%\npm-cache 2>nul | find "File(s)"Problem
After running npm cache clean --force, installation became extremely slow:
npm cache clean --force
# npm warn using --force Recommended protections deprecated.
npm install
# Takes 10+ minutes to complete
# Previously took 30 secondsThe cache was cleared, so npm had to download every package from scratch.
Analysis
The npm cache stores:
%LOCALAPPDATA%\npm-cache\_cacache\ ← Package tarballs
%LOCALAPPDATA%\npm-cache\_logs\ ← Install logs
%LOCALAPPDATA%\npm-cache\_npx\ ← npx cacheWhen the cache is empty, npm must:
- Resolve all dependencies from the registry
- Download all tarballs
- Extract and install them
This is much slower than using cached packages.
Solution
Step 1: Check current cache status
npm cache verify
# Cache verified and content verifiedStep 2: Use npm ci for CI/CD (not npm install)
# In CI/CD, always use ci
npm ci
# npm ci does:
# 1. Deletes node_modules
# 2. Installs exactly from package-lock.json
# 3. Uses cache when available
# 4. Is faster and more reliableStep 3: Configure cache location
# Set custom cache directory
npm config set cache /path/to/fast/ssd/npm-cache
# Check current cache location
npm config get cache
# C:\Users\user\AppData\Local\npm-cacheStep 4: Use a local mirror
# Use Verdaccio as local registry
npm install -g verdaccio
verdaccio &
# Configure npm to use local registry
npm set registry http://localhost:4873/
# Or use a specific registry for your organization
npm config set @mycompany:registry https://npm.mycompany.com/Step 5: Parallel downloads
# Increase network concurrency
npm config set maxsockets 15
# Use different registry for faster access
npm config set registry https://registry.npmmirror.com/Step 6: Optimize package-lock.json
// package.json - use exact versions for faster resolution
{
"dependencies": {
"express": "4.18.2", // Exact version - fast
"lodash": "^4.17.21", // Range - needs resolution
"react": "18.2.0" // Exact version - fast
}
}Step 7: Cache management script
#!/bin/bash
# optimize-npm.sh
echo "Verifying npm cache..."
npm cache verify
echo "Cleaning old logs..."
rm -rf $(npm config get cache)/_logs/*
echo "Checking cache size..."
du -sh $(npm config get cache)
echo "Installing dependencies..."
npm ci --prefer-offline --no-audit --no-fund
echo "Done!"# Windows version
Write-Host "Verifying npm cache..."
npm cache verify
Write-Host "Cleaning old logs..."
Remove-Item -Path "$(npm config get cache)\_logs\*" -Force -ErrorAction SilentlyContinue
Write-Host "Installing dependencies..."
npm ci --prefer-offline --no-audit --no-fund
Write-Host "Done!"Lessons Learned
- Never use
npm cache clean --forcein CI/CD - Usenpm ciinstead - Use
--prefer-offlineto prioritize cached packages - Set up a local registry for teams with slow internet connections
- Use exact versions in package.json when possible
- Monitor cache size - Clean old packages periodically with
npm cache verify
This blog does not accept any external sponsorships, affiliate marketing, or ad revenue.