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