跪拜 Guibai
← All articles
Backend · Linux · Frontend

npm Says 0-Exit, Disk Says 370M: The Hidden .name-XXXXXXXX Trash That Survives prune

By 用户11293611219 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Anyone using managed CI containers, sandboxes, or security-instrumented Node runtimes can silently accumulate stale dependencies and disk bloat despite npm's clean exit codes. The failure mode is environment-specific, so scripts that work locally may quietly fail in CI, and knowing where the delete path diverges is the difference between a 2-second false success and a real cleanup.

Summary

A managed CI cleanup with `npm prune` exited 0 and printed "up to date," yet `du -sh node_modules` still showed 370M. The culprit: npm's two-phase delete first atomically renames the target directory, then recursively deletes it. A safe-delete shim in the environment blocks the second phase when a batch exceeds ~50 targets, so directories get renamed to hidden `.pkg-XXXXXXXX` names and then abandoned. npm treats the block as a non-error and returns success, creating three stacked illusions that the deletion worked.

The problem hides from ordinary checks because `ls` without `-a` doesn't show the hidden directories, and scoped `@scope/` directories are often skipped entirely by `prune`. In the reported case, 84 hidden trash directories plus a 217M `@tencent-ai` scoped folder remained. The fix relies on explicit `du` and hidden-directory checks, manual deletion with Git Bash `rm.exe` (which bypasses the shim), and cross-referencing `package-lock.json` to identify removable residue. After applying the steps, the node_modules size dropped from 370M to 80M.

Takeaways
— `npm prune` exits 0 and reports 'up to date' even when the recursive delete phase is blocked by a safe-delete shim.
— Blocked deletions leave directories renamed to hidden `.name-XXXXXXXX` patterns, invisible to a plain `ls` but counting toward disk usage.
— Scoped `@scope/` directories are often skipped entirely by `npm prune`; a 217M `@tencent-ai` folder remained after the command ran.
— In the reported case, 84 hidden trash directories remained, and disk usage stayed at 370M before cleanup; after the fix it dropped to 80M.
— The safe-delete shim blocks Node's `fs.rm` but not Git Bash's `rm.exe`, so manual deletion works while scripted deletion fails, sending debuggers off toward permission issues.
— Cross-reference `package-lock.json` to decide which directories are safe to remove: anything not listed is residue, anything listed must stay.
— A functional check like `node -e "require('ffmpeg-static')"` verifies the binary actually loads, not just that a directory exists.
Conclusions

The silent failure stems from npm's exit code reflecting only the rename phase, not the recursive delete; an exit 0 is a contract about the operation's start, not its completion.

Deletion shims that guard against mass deletions create a blind spot for node_modules, where huge scoped directories routinely survive prune and get misattributed to permission or path problems.

Environment-specific behavior like Git Bash rm bypassing the shim means the same command works or fails depending on the shell, which makes debugging far harder because the toolchain, not the logic, is the variable.

Concepts & terms
safe-delete shim
A wrapper injected into file-deletion operations in sandboxes, CI containers, or managed runtimes that intercepts and blocks batch recursive deletes (typically when they exceed a threshold like 50 targets) to prevent accidental mass data loss. It silently blocks the operation without signaling an error to the calling process.
two-phase npm delete
npm's uninstall mechanism: first atomically renames the target directory to a temporary hidden name within the same parent (making it logically disappear), then recursively deletes the renamed directory. If the second phase is blocked, the hidden directory remains, and npm still reports success based on the rename.
From the discussion
Featured comments
去吧波波鸟

Exit code 0 can still be fake, this is infuriating. The ls -a trick does work, those hidden 84 directories are exposed at a glance. From now on, after deleting dependencies, I'll first check with du -sh to compare numbers, that's more reliable than looking at the output.

云屿智研

I rely entirely on AI now, feeling like I'm getting dumber, whatever happens AI solves it. (facepalm)

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗