npm install -g fails with EACCES: permission denied (Mac/Linux)
#142Error
npm error code EACCES npm error syscall mkdir/rename npm error path /usr/local/lib/node_modules/... npm error Error: EACCES: permission denied
Why it happens, and why this works
With the system Node, the global prefix is /usr/local (or /usr), which only root can write to. A user-owned prefix or a version manager moves global installs under your home directory, so no elevated permissions are needed.
Fix
Don't use sudo; make global installs go to a folder you own. 1. Easiest: install Node through a version manager (nvm, fnm, volta). It puts global packages in your home folder, so -g works without sudo. 2. Or point npm's prefix at your home: mkdir -p ~/.npm-global && npm config set prefix ~/.npm-global, then add export PATH=~/.npm-global/bin:$PATH to ~/.zshrc or ~/.bashrc and open a new terminal. 3. If a previous sudo install left root-owned files in ~/.npm: sudo chown -R $(whoami) ~/.npm 4. For one-off tools, npx <tool> avoids a global install.
Did it work?
No reports yetRelated fixes
npm install fails with ERESOLVE unable to resolve dependency tree (peer dependency conflict)
#36npm error code ERESOLVE npm error ERESOLVE unable to resolve dependency tree npm error Could not resolve dependency: peer react@"^18.3.1" from react-dom@18.3.1
Error: Cannot find module 'express' (package not installed)
#58Error: Cannot find module 'express' Require stack: - /app/app.js code: 'MODULE_NOT_FOUND'
npm publish of a new package fails with 404 Not Found on PUT, then 403 two-factor required
#21npm publish fails: 404 Not Found - PUT https://registry.npmjs.org/<package> - Not found. "could not be found or you do not have permission to access it" (first publish of a new package). After logging in again: 403 Forbidden - Two-factor authentication or granular access token with bypass 2fa enabled is required to publish packages.