The ENOTEMPTY error occurs when npm cannot remove a directory because it still contains files, typically due to interrupted installations, file locks from running processes, or corrupted node_modules state.
The ENOTEMPTY error occurs when npm encounters a non-empty directory during the installation process and cannot complete the expected operation. The "ENOTEMPTY" error code is a POSIX standard meaning "Error, Not Empty"—npm is trying to replace, rename, or remove a directory but finds it still contains files or subdirectories that prevent the operation from completing. This typically happens when npm's directory management gets disrupted during dependency installation or updates. The rmdir operation in the error message indicates npm was specifically trying to remove an empty directory when it encountered leftover files. The error becomes especially problematic because npm's internal cleanup process depends on being able to remove and replace package directories atomically. When a directory remains non-empty (whether due to file locks, previous failed installations, or concurrent operations), npm cannot proceed.
Close any development servers, editors, or watchers that might have file handles open:
# Kill webpack-dev-server and similar processes
pkill -f "webpack-dev-server"
pkill -f "next dev"
pkill -f "node"
# On macOS, force quit VS Code if needed
killall "Visual Studio Code"
killall "Code"File locks prevent npm from renaming or removing directories. Stopping the processes releases these locks.
Completely remove the corrupted dependency tree and lock file, then reinstall from scratch:
# Remove node_modules directory
rm -rf node_modules
# Remove lock file to ensure fresh dependency resolution
rm -f package-lock.json
# Clear the npm cache
npm cache clean --forceThis eliminates all cached state and incomplete files, forcing npm to download and extract everything fresh.
Run a fresh installation:
npm installIf the error persists, try using npm ci instead, which is more deterministic:
npm cinpm ci (clean install) is optimized for CI/CD environments and enforces stricter consistency checks than npm install.
If the error reoccurs, manually clean up npm's temporary directories:
# List hidden directories to inspect
ls -la node_modules/ | grep "^\."
# Remove all npm temporary directories
rm -rf node_modules/.*-*
# On Linux/macOS, be specific:
find node_modules -maxdepth 1 -name ".*" -type d -exec rm -rf {} \; 2>/dev/nullnpm creates temporary directories with random suffixes during extraction. If a crash occurs mid-rename, these orphaned directories block subsequent installs.
For persistent issues, clear all caches and temporary files:
# Clear npm cache completely
npm cache clean --force
# On macOS, clear System caches
sudo rm -rf ~/Library/Caches/npm
sudo rm -rf ~/.npm
# On Linux, clear npm home cache
rm -rf ~/.npm
rm -rf ~/.cache/npmCorrupted cache entries from failed downloads can cause subsequent installs to fail.
If standard installation still fails, use compatibility flags:
# Skip symlink creation (useful on macOS with strict file permissions)
npm install --no-bin-links
# Use legacy peer dependency resolution
npm install --legacy-peer-deps
# Combine both flags if needed
npm install --no-bin-links --legacy-peer-deps--no-bin-links avoids the rename operation that triggers ENOTEMPTY. --legacy-peer-deps reduces package conflicts that might cause incomplete installations.
macOS Sonoma specific issue: npm 10+ and Node 22+ use stricter file locking on APFS filesystems. If you encounter this error on Sonoma, --no-bin-links is highly effective because it bypasses the atomic rename operation that triggers the ENOTEMPTY error.
Docker builds: If this error occurs during Docker builds, add npm cache clean --force before npm install in your Dockerfile.
CI/CD environments: Use npm ci instead of npm install in CI pipelines. It's faster, more reliable, and respects exact versions from package-lock.json. If ENOTEMPTY occurs in CI, ensure the environment cleans node_modules between runs.
Behind corporate proxies: If npm cache corruption occurs due to proxy interception, add proxy credentials explicitly with npm config set proxy and npm config set https-proxy.
npm ERR! code ENOAUDIT npm ERR! Audit endpoint not supported
How to fix "npm ERR! code ENOAUDIT - Audit endpoint not supported"
npm ERR! code EBADDEVENGINES npm ERR! devEngines.runtime incompatible with current node version
How to fix "npm ERR! code EBADDEVENGINES - devEngines.runtime incompatible with current node version"
npm ERR! code ETOOMANYARGS npm ERR! Too many arguments
How to fix "npm ERR! code ETOOMANYARGS - Too many arguments"
npm ERR! code EINVALIDTAGNAME npm ERR! Invalid tag name: tag names cannot contain spaces
How to fix "npm ERR! code EINVALIDTAGNAME - tag names cannot contain spaces"
npm ERR! code E400 npm ERR! 400 Bad Request
How to fix "npm ERR! code E400 - 400 Bad Request" error