The ENOENT cache error occurs when npm's cache directory is missing or inaccessible. Clearing and rebuilding the cache usually resolves this issue.
Fixes npm ERR! ENOENT: no such file or directory
npm config get cache
# Check if directory exists
ls -la $(npm config get cache)npm config get cache# Check if directory existsls -la $(npm config get cache)npm ERR! ENOENT: no such file or directory
npm maintains a cache of downloaded packages to speed up future installations. When the cache directory or specific cached files are missing or corrupted, npm may fail with ENOENT errors during cache operations. This can happen after system cleanups, disk errors, or when cache paths are misconfigured.
Check cache configuration:
npm config get cache
# Check if directory exists
ls -la $(npm config get cache)Clear and rebuild:
npm cache clean --force
# Verify cache
npm cache verifySet to default or new location:
# Reset to default
npm config delete cache
# Or set new location
npm config set cache /path/to/new/cacheEnsure proper access:
# Check permissions
ls -la ~/.npm
# Fix if needed
sudo chown -R $(whoami) ~/.npm
chmod -R u+rwX ~/.npmManually create if missing:
mkdir -p ~/.npm/_cacache
mkdir -p ~/.npm/_logsEnsure cache persistence:
# Docker volume
volumes:
- npm-cache:/root/.npm
# GitHub Actions
- uses: actions/cache@v3
with:
path: ~/.npm
key: npm-cache-\${{ hashFiles('**/package-lock.json') }}npm's cache is content-addressable, making it safe to delete entirely. The cache grows unbounded - periodic npm cache clean may be needed. In CI, cache warming speeds up builds but stale caches can cause issues. Multi-user systems should have separate caches or use npm's --cache flag per-user. Docker builds benefit from caching the npm cache directory.