How to fix npm ERR! code ENOTEMPTY npm ERR! ENOTEMPTY: directory not empty, rmdir in npm

npmINTERMEDIATEMEDIUMUpdated Dec 5, 2025· 220 views

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.

Quick fix

Fixes npm ERR! code ENOTEMPTY npm ERR! ENOTEMPTY: directory not empty, rmdir

# 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"
  1. Stop any running processes accessing node_modules
  2. Delete node_modules and package-lock.json
  3. Reinstall dependencies
  4. Remove hidden temporary directories
npm — error
# 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"
npm ERR! code ENOTEMPTY
npm ERR! ENOTEMPTY: directory not empty, rmdir

What this error means

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.

Typical symptoms

  • npm install halts with 'ENOTEMPTY: directory not empty, rmdir' error message
  • Installation fails midway when downloading or extracting dependencies
  • Partial files or .DELETE suffixed folders appear in node_modules/
  • Hidden temporary directories like node_modules/.<package>-<random-string> remain after failed installs
  • Error occurs even on fresh clone or after deleting some packages
  • Running npm install repeatedly shows the same error without progress

Common causes

  1. Interrupted previous installation: A prior npm install was terminated (Ctrl+C, network disconnection, process crash) before cleanup completed
  2. File locks from running processes: An active webpack dev server, IDE, or file watcher has files in node_modules open
  3. Corrupted npm cache: Failed downloads or network interruptions left incomplete package metadata in the npm cache
  4. Concurrent package manager operations: Running multiple npm/yarn installations simultaneously causes race conditions
  5. macOS Sonoma APFS locking issues: Newer versions with strict file locking leave hidden files that prevent directory removal
  6. Manual changes to node_modules: Files manually added or moved within node_modules create unexpected directory structure

How to fix "npm ERR! code ENOTEMPTY npm ERR! ENOTEMPTY: directory not empty, rmdir"

1Stop any running processes accessing node_modules

Close any development servers, editors, or watchers that might have file handles open:

bash
# 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.

2Delete node_modules and package-lock.json

Completely remove the corrupted dependency tree and lock file, then reinstall from scratch:

bash
# 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 --force

This eliminates all cached state and incomplete files, forcing npm to download and extract everything fresh.

3Reinstall dependencies

Run a fresh installation:

bash
npm install

If the error persists, try using npm ci instead, which is more deterministic:

bash
npm ci

npm ci (clean install) is optimized for CI/CD environments and enforces stricter consistency checks than npm install.

4Remove hidden temporary directories

If the error reoccurs, manually clean up npm's temporary directories:

bash
# 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/null

npm creates temporary directories with random suffixes during extraction. If a crash occurs mid-rename, these orphaned directories block subsequent installs.

5Clear system and npm caches completely

For persistent issues, clear all caches and temporary files:

bash
# 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/npm

Corrupted cache entries from failed downloads can cause subsequent installs to fail.

6Use npm install with --no-bin-links flag

If standard installation still fails, use compatibility flags:

bash
# 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.

Advanced notes

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.

Frequently asked questions

What does "npm ERR! code ENOTEMPTY npm ERR! ENOTEMPTY: directory not empty, rmdir" mean?
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.
How do I fix "npm ERR! code ENOTEMPTY npm ERR! ENOTEMPTY: directory not empty, rmdir"?
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.
What causes "npm ERR! code ENOTEMPTY npm ERR! ENOTEMPTY: directory not empty, rmdir"?
Interrupted previous installation: A prior npm install was terminated (Ctrl+C, network disconnection, process crash) before cleanup completed File locks from running processes: An active webpack dev server, IDE, or file watcher has files in node_modules open Corrupted npm cache: Failed downloads or network interruptions left incomplete package metadata in the npm cache
Is the "npm ERR! code ENOTEMPTY npm ERR! ENOTEMPTY: directory not empty, rmdir" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading