How to fix npm ERR! code EPRUNE npm ERR! Failed to remove extraneous packages in npm

npmINTERMEDIATEMEDIUMUpdated Dec 5, 2025· 193 views

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
  1. Close all applications accessing node_modules
  2. Disable antivirus temporarily and retry
  3. Clear npm cache and retry
  4. Perform a clean install
npm — error
# On Windows, check what's locking files:
tasklist | findstr node
# Kill any node processes:
taskkill /F /IM node.exe
npm ERR! code EPRUNE
npm 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

  1. File locks held by running processes (VS Code, antivirus, IDE TypeScript compiler watching node_modules)
  2. Permission issues: user running npm lacks write/delete permissions on node_modules directory
  3. Windows-specific file system locks or long path issues preventing deletion of nested directories
  4. Corrupted node_modules structure from interrupted npm operations or failed installs
  5. npm cache corruption or version mismatch between npm and lock file
  6. 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"

1Close all applications accessing node_modules

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.

bash
# On Windows, check what's locking files:
tasklist | findstr node

# Kill any node processes:
taskkill /F /IM node.exe

Wait a few seconds, then try your npm command again.

2Disable antivirus temporarily and retry

Windows Defender, McAfee, Avast, and other antivirus software can block npm from deleting files. Temporarily disable your antivirus and attempt the operation again.

bash
# Try the operation that was failing:
npm prune
npm install

If this succeeds, add your project directory to your antivirus exclusion list and re-enable the software.

3Clear npm cache and retry

npm cache corruption can prevent proper cleanup operations.

bash
# Clear the npm cache
npm cache clean --force

# Then retry your operation
npm prune
npm install
4Perform a clean install

If the above steps don't work, completely remove node_modules and reinstall.

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

On Windows PowerShell:

powershell
Remove-Item -Recurse -Force node_modules
Remove-Item package-lock.json
npm cache clean --force
npm install
5Use npm ci instead of npm install in CI/CD

In continuous integration environments, use npm ci (clean install) instead of npm install.

bash
# In CI/CD pipelines, use:
npm ci

# Instead of:
npm install

npm 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