npm ERR! code ENOTEMPTY
This error matches known, documented patterns with reliable solutions.
Quick Fix (Most Common Solution)
- Manually delete the directory
- Run npm cache clean --force
Seeing "npm ERR! code ENOTEMPTY"? This error can be frustrating, but it's usually fixable. It typically affects your development workflow or system. Below you'll find clear, step-by-step solutions to resolve this issue.
What This Error Means
npm expected an empty directory but found files.
Frequently documented in developer and vendor support forums.
Not affiliated with browser, OS, or device manufacturers.
New here? Learn why exact error messages matter →
Common Causes
- Failed uninstall left files
- Manual file modifications
- Race condition
How to Fix
- Manually delete the directory
- Run npm cache clean --force
- Delete node_modules and reinstall
Last reviewed: March 2026 How we review solutions
Why This Happens
ENOTEMPTY is a POSIX error code meaning npm tried to remove or rename a directory but the directory still contains files. During npm install, npm frequently moves and replaces directories inside node_modules to update packages to new versions. If a file inside the target directory is locked by another process (an IDE indexer, a file watcher, antivirus software, or another npm process), npm cannot remove the directory and the rename operation fails. On Windows, this error is significantly more common because of mandatory file locking—any file opened by any process cannot be removed. On macOS and Linux, the error usually indicates a race condition where npm tried to operate on a directory that another concurrent npm process is also modifying. Corrupted node_modules state from a previously interrupted installation is another common cause: if npm was killed mid-install (Ctrl+C, power loss, OOM kill), the partially-written directory structure may contain files that npm does not expect, preventing clean updates on the next install. The fix is almost always to delete node_modules entirely and start fresh.
Quick Diagnostic Checklist
- Close all IDEs and file watchers before running npm install
- Check for stale Node processes: ps aux | grep node
- Delete node_modules and reinstall: rm -rf node_modules && npm install
- On Windows, check for file locks using Process Explorer
- If in CI, ensure no parallel jobs share the same node_modules directory
Fixing ENOTEMPTY during npm install
# Error output:
# npm ERR! code ENOTEMPTY
# npm ERR! syscall rename
# npm ERR! path /home/user/project/node_modules/webpack
# npm ERR! dest /home/user/project/node_modules/.webpack-XXXX
# npm ERR! errno -39
# npm ERR! ENOTEMPTY: directory not empty, rename
# Step 1: Check for processes using node_modules
$ lsof +D node_modules 2>/dev/null | head -10
# node 1234 user txt REG ... node_modules/.cache/webpack/...
# ^-- VS Code or dev server has files open
# Step 2: Close the processes or your IDE
$ kill 1234 # Or just close VS Code
# Step 3: Clean delete and reinstall
$ rm -rf node_modules
$ rm package-lock.json # Optional: regenerate lockfile
$ npm install
# Step 4: If rm -rf fails on Windows
$ rmdir /s /q node_modules
# If that fails too, restart your computer to release all locks
# Then: rmdir /s /q node_modules && npm install
# Prevention: Use npm ci for clean installs
$ npm ci # Deletes node_modules first, then installs from lockfile
Edge Cases & Unusual Scenarios
npm ci vs npm install behavior
npm ci always deletes node_modules before installing, avoiding ENOTEMPTY entirely. Use npm ci in CI/CD pipelines and when you want a guaranteed clean state. npm install tries to update in-place, which is where rename conflicts occur.
Symbolic links inside node_modules
npm link creates symbolic links inside node_modules. If the linked package was deleted or moved, the stale symlink prevents directory operations. Remove stale symlinks with find node_modules -xtype l -delete before reinstalling.
Monorepo with hoisted dependencies
In npm workspaces, packages are hoisted to the root node_modules. If a workspace package deletes its own node_modules while the root is being modified, rename operations can fail. Always run npm install from the workspace root.
Optional follow-up
Some users ask whether saving fixes for recurring errors would be useful when the same issue appears again.
Was this explanation helpful?
Frequently Asked Questions
Can I ignore this error?
No, it will prevent proper installation.
What directory is the problem?
Check the full error message for the path.
Why does npm try to rename directories instead of updating files in place?
npm uses an atomic rename strategy for safety: it creates the new version in a temporary directory, then renames it to replace the old one. This prevents partially-installed packages if the process is interrupted. The downside is that renames fail if the target directory is not empty or is locked.
Is ENOTEMPTY the same as EEXIST?
No. EEXIST means a file or directory already exists where npm wants to create one. ENOTEMPTY means a directory cannot be removed or renamed because it still contains files. The causes overlap (both involve stale state), but the underlying OS operation is different.
How do I prevent ENOTEMPTY in automated builds?
Use npm ci instead of npm install. npm ci always starts with a clean node_modules and installs exactly what the lockfile specifies, eliminating rename conflicts entirely.
Related Resources
Also Known As
- npm install error
- npm dependency error
- node package manager error
- npm ERR! message
Common Search Variations
- "npm install not working"
- "npm ERR how to fix"
- "node modules error fix"
- "npm install failed what to do"
- "npm dependency conflict solution"
- "why does npm install fail"
Related Errors
Still Stuck?
Paste a different error message or upload a screenshot to get help instantly.