npm cannot remove its temp .DELETE/.staging folders because they are root-owned or locked. Clean them, move to a user-writable prefix, and prevent AV or other processes from holding the files.
npm stages global installs in temporary .staging folders and cleans them with unlink/rename. If those folders are owned by root (from earlier sudo installs) or locked by AV/EDR, npm's unlink fails and the install aborts, leaving partially staged content. The problem is common on Windows/WSL when Defender scans node_modules, and on macOS/Linux when mixing sudo and non-sudo installs leaves root-owned temp artifacts under the global prefix.
sudo rm -rf $(npm config get prefix)/lib/node_modules/.staging \
$(npm config get prefix)/lib/node_modules/.*.DELETE 2>/dev/null || truemkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
export PATH="$HOME/.npm-global/bin:$PATH"
# or install Node with nvm to keep globals in $HOMEsudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}Add your global prefix to AV exclusions so unlink/rename is not blocked. Close VS Code or terminals that keep handles open.
npm cache clean --force
npm install -g <package>In Docker/CI, ensure npm runs as the filesystem owner or set prefix to a workspace path owned by the job user; root-owned layers will block unlink. Windows/WSL users often hit EPERM/EACCES from Defender scans—Developer Mode plus AV exclusions reduce locks on temp files. Avoid mixing sudo and user installs; it consistently leaves root-owned staging folders that require manual cleanup.
npm notice access token expired or revoked. Please try logging in again.
Token has expired - npm authentication failure
npm ERR! code EAI_AGAIN
How to fix "EAI_AGAIN" in npm
npm error code E403 npm error 403 Forbidden - PUT https://registry.npmjs.org/<package>
How to fix 'E403 Forbidden' error in npm
npm ERR! code EUSAGE npm ERR! Usage error
How to fix "npm ERR! code EUSAGE" in Node.js projects
npm ERR! code E401 npm ERR! 401 Unauthorized
How to fix "E401 Unauthorized" in npm