[Bug] Auto-update fails with npm ENOTEMPTY rename error
Bug Description I keep getting auto-update errors. When I quit and attempt to do the update myself, it fails because of some kind of corruption of the global npm package install. I then have to manually delete the entire package and do a fresh install.
This is the message I get in the Claude Code terminal at the bottom in red text:
✗ Auto-update failed · Try claude doctor or npm i -g
@anthropic-ai/claude-code
This is the error I get when I exit and attempt to update manually:
❯ npm i -g @anthropic-ai/claude-code
npm error code ENOTEMPTY
npm error syscall rename
npm error path /Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code
npm error dest /Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/.claude-code-0ePLndiR
npm error errno -66
npm error ENOTEMPTY: directory not empty, rename '/Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code' -> '/Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/.claude-code-0ePLndiR'
npm error A complete log of this run can be found in: /Users/willd/.npm/_logs/2025-09-10T00_42_19_467Z-debug-0.log
It only works again if it rm -rf the claude-code package directory and then run a fresh install.
Environment Info
- Platform: darwin
- Terminal: iTerm.app
- Version: 1.0.109
- Feedback ID: d27b86f3-3c19-4ec8-8f2f-25c081fd7abe
Errors
[{"error":"Error: Request was aborted.\n at i61._createMessage (file:///Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code/cli.js:971:7955)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","timestamp":"2025-09-10T00:03:30.151Z"},{"error":"SyntaxError: Unexpected token 'I', \"I apologiz\"... is not valid JSON\n at JSON.parse (<anonymous>)\n at file:///Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code/cli.js:733:9932\n at Q (file:///Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code/cli.js:713:13288)\n at at2 (file:///Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code/cli.js:1095:1982)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)","timestamp":"2025-09-10T00:19:44.828Z"},{"error":"Error: Failed to install new version of claude: npm error code ENOTEMPTY\nnpm error syscall rename\nnpm error path /Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code\nnpm error dest /Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/.claude-code-0ePLndiR\nnpm error errno -66\nnpm error ENOTEMPTY: directory not empty, rename '/Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code' -> '/Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/.claude-code-0ePLndiR'\nnpm error A complete log of this run can be found in: /Users/<username_redacted>/.npm/_logs/2025-09-10T00_28_21_539Z-debug-0.log\n at IG1 (file:///Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code/cli.js:1612:393)\n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n at async Object.current (file:///Users/<username_redacted>/.nvm/versions/node/v20.19.4/lib/node_modules/@anthropic-ai/claude-code/cli.js:3563:12159)","timestamp":"2025-09-10T00:28:22.061Z"}]
Found 3 possible duplicate issues:
- https://github.com/anthropic-ai/claude-code/issues/996
- https://github.com/anthropic-ai/claude-code/issues/6864
- https://github.com/anthropic-ai/claude-code/issues/7188
This issue will be automatically closed as a duplicate in 3 days.
- If your issue is a duplicate, please close it and 👍 the existing issue instead
- To prevent auto-closure, add a comment or 👎 this comment
🤖 Generated with Claude Code
The bot is generating broken links.
It is using anthropic-ai as the organization, but it should be anthropics
Root Cause Analysis & Permanent Fix
I've experienced this exact same issue and conducted a thorough investigation. Here's the root cause and permanent solution:
Root Cause
The ENOTEMPTY error occurs because auto-updates attempt to run while Claude Code is actively running, causing file locks that prevent npm from renaming the installation directory.
Technical sequence:
- Auto-update triggers while Claude process is active
- npm plans to rename
/node_modules/@anthropic-ai/claude-code→/.claude-code-[randomstring] - Running Claude process has file locks on executable files
- Filesystem rename operation fails with
ENOTEMPTY - Leaves corrupted temporary directories that block future updates
Evidence
From npm debug logs, the failure happens at the rename step:
silly reify moves {
'/Users/[user]/.nvm/versions/node/v20.17.0/lib/node_modules/@anthropic-ai/claude-code':
'/Users/[user]/.nvm/versions/node/v20.17.0/lib/node_modules/@anthropic-ai/.claude-code-ArTm9x67'
}
Permanent Solution
The issue requires process-aware updates. I've implemented this fix:
1. Disable problematic auto-updates:
claude config set autoUpdates false --global
2. Create safe update script:
#!/bin/bash
# Stop Claude processes before updating
pkill -f "claude" 2>/dev/null
sleep 2
# Clean corrupted temp directories
rm -rf /Users/$USER/.nvm/versions/node/*/lib/node_modules/@anthropic-ai/.claude-code-*
# Update safely
npm i -g @anthropic-ai/claude-code@latest
3. Weekly maintenance instead of daily failures: Use controlled updates that properly handle process termination.
Environment Details
- OS: macOS 15.6 (24G84)
- Claude Code: 1.0.111
- Node: v20.17.0 (via nvm)
- npm: 10.8.2
Recommended Fix for Maintainers
Auto-updates should:
- Check for running Claude processes before attempting update
- Gracefully terminate or schedule update for next startup
- Clean up temp directories from failed updates
- Implement retry logic with exponential backoff
This is a common issue with self-updating applications and needs special handling to avoid file lock conflicts.
After implementing the above solution, the daily auto-update failures completely stopped.
For me I had to rm -rf ~/.npm/_npx, as I was using it with Vibe Kanban, which invokes npx directly
The ENOTEMPTY error occurs because auto-updates attempt to run while Claude Code is actively running, causing file locks that prevent npm from renaming the installation directory.
That's now how file locking on Unix works. Deleting (or equivalently atomically replacing) files or directories with open file descriptors is not an issue, and is in fact how updates are often handled.
Checking for running processes of the application to be updated is still a good idea, but is only required if it doesn't open all resource files it needs at startup time. If it's not done, there might be issues with currently running instances, but never with the update process itself.
ENOTEMPTY happens when trying to delete a non-empty directory or when trying to replace a non-empty directory with another.
This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.
Still relevant
Same for me:
me@mycomp someFolder % ps -ef | grep -i claude
502 72179 65368 0 9:17AM ttys009 0:00.00 grep -i claude
me@mycomp someFolder %
me@mycomp someFolder % npm i -g @anthropic-ai/claude-code
(node:67029) ExperimentalWarning: CommonJS module /opt/homebrew/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /opt/homebrew/lib/node_modules/npm/node_modules/supports-color/index.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
npm error code ENOTEMPTY
npm error syscall rename
npm error path /opt/homebrew/lib/node_modules/@anthropic-ai/claude-code
npm error dest /opt/homebrew/lib/node_modules/@anthropic-ai/.claude-code-2DTsDk1V
npm error errno -66
npm error ENOTEMPTY: directory not empty, rename '/opt/homebrew/lib/node_modules/@anthropic-ai/claude-code' -> '/opt/homebrew/lib/node_modules/@anthropic-ai/.claude-code-2DTsDk1V'
npm error A complete log of this run can be found in: /Users/me/.npm/_logs/2025-12-18T07_10_21_503Z-debug-0.log