How to fix npm ERR! code EPRUNE npm ERR! Failed to remove extraneous packages in npm
The npm prune error occurs when npm cannot delete packages from your node_modules directory that aren't listed in package.json, typically due to file locks, permission issues, or corrupted node_modules structures.
Quick fix
Fixes npm ERR! code EPRUNE npm ERR! Failed to remove extraneous packages
# On Windows, check what's locking files:
tasklist | findstr node
# Kill any node processes:
taskkill /F /IM node.exe- Close all applications accessing node_modules
- Disable antivirus temporarily and retry
- Clear npm cache and retry
- Perform a clean install
# On Windows, check what's locking files:tasklist | findstr node# Kill any node processes:taskkill /F /IM node.exenpm ERR! code EPRUNEnpm ERR! Failed to remove extraneous packages
What this error means
The error "Failed to remove extraneous packages" occurs when npm cannot delete packages from your node_modules directory that aren't listed in your package.json file. This typically happens during npm prune operations or when npm tries to clean up dependencies. Extraneous packages are installed dependencies that npm detects as no longer needed—they may have been manually added, left behind from a previous installation, or installed without proper --save flags. The failure to remove them usually stems from permission issues, file locks, or corrupted node_modules structures that prevent npm from completing the cleanup operation. This issue is most common on Windows systems where file locking is more aggressive, and in CI/CD environments with restrictive permissions.
Typical symptoms
- npm prune command hangs or fails with 'Failed to remove extraneous packages' message
- npm install or npm ci operations fail when trying to clean up old dependencies
- Specific subdirectories in node_modules (like .bin folders) cannot be deleted
- Error occurs intermittently on Windows or in CI/CD environments
- Antivirus software or IDE file locks prevent the operation from completing
Common causes
- File locks held by running processes (VS Code, antivirus, IDE TypeScript compiler watching node_modules)
- Permission issues: user running npm lacks write/delete permissions on node_modules directory
- Windows-specific file system locks or long path issues preventing deletion of nested directories
- Corrupted node_modules structure from interrupted npm operations or failed installs
- npm cache corruption or version mismatch between npm and lock file
- Symlinked dependencies or hoisted packages creating circular references that prune cannot resolve
How to fix "npm ERR! code EPRUNE npm ERR! Failed to remove extraneous packages"
The most common cause is file locks from running processes. Close your IDE (especially VS Code), terminal windows, and any file managers viewing the project directory.
# On Windows, check what's locking files:
tasklist | findstr node
# Kill any node processes:
taskkill /F /IM node.exeWait a few seconds, then try your npm command again.
Windows Defender, McAfee, Avast, and other antivirus software can block npm from deleting files. Temporarily disable your antivirus and attempt the operation again.
# Try the operation that was failing:
npm prune
npm installIf this succeeds, add your project directory to your antivirus exclusion list and re-enable the software.
npm cache corruption can prevent proper cleanup operations.
# Clear the npm cache
npm cache clean --force
# Then retry your operation
npm prune
npm installIf the above steps don't work, completely remove node_modules and reinstall.
# Remove node_modules and lock file
rm -rf node_modules
rm package-lock.json
# Clear npm cache
npm cache clean --force
# Reinstall all dependencies
npm installOn Windows PowerShell:
Remove-Item -Recurse -Force node_modules
Remove-Item package-lock.json
npm cache clean --force
npm installIn continuous integration environments, use npm ci (clean install) instead of npm install.
# In CI/CD pipelines, use:
npm ci
# Instead of:
npm installnpm ci uses the exact versions from package-lock.json and is more reliable in automated environments.
Advanced notes
For developers encountering this in CI/CD pipelines (GitHub Actions, GitLab CI, Jenkins): The error often occurs because the runner environment has restrictive file permissions. Solutions include using npm ci with --prefer-offline flag, adding npm cache clean --force before install steps, and configuring .npmrc with legacy-peer-deps=true if needed.
For Windows developers using WSL: Keeping your project files in the WSL filesystem (e.g., ~/project not /mnt/c/Users/...) dramatically improves performance and reliability, as the Windows file system introduces additional locking complications.
For monorepo/workspace setups: npm prune has known issues with workspaces where dev dependencies in child packages don't get properly removed. Consider using npm prune --production at the root level only.
Frequently asked questions
- What does "npm ERR! code EPRUNE npm ERR! Failed to remove extraneous packages" mean?
- json file. This typically happens during npm prune operations or when npm tries to clean up dependencies. Extraneous packages are installed dependencies that npm detects as no longer needed—they may have been manually added, left behind from a previous installation, or installed without proper --save flags.
- How do I fix "npm ERR! code EPRUNE npm ERR! Failed to remove extraneous packages"?
- The npm prune error occurs when npm cannot delete packages from your node_modules directory that aren't listed in package.json, typically due to file locks, permission issues, or corrupted node_modules structures.
- What causes "npm ERR! code EPRUNE npm ERR! Failed to remove extraneous packages"?
- File locks held by running processes (VS Code, antivirus, IDE TypeScript compiler watching node_modules) Permission issues: user running npm lacks write/delete permissions on node_modules directory Windows-specific file system locks or long path issues preventing deletion of nested directories
- Is the "npm ERR! code EPRUNE npm ERR! Failed to remove extraneous packages" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- 📚npm Official Documentation
- 📖npm prune documentation(Official Docs)
- 💬Stack Overflow: npm(Stack Overflow)