npm EACCES Permission Error — Global Package Install Permission Denied
Warningpackage
Overview
Fix npm EACCES error when installing global packages fails because the default global directory requires root permissions that npm should not use.
Key Details
- npm global packages install to /usr/local/lib/node_modules by default which requires root
- Running 'sudo npm install -g' works but creates permission issues for subsequent commands
- The recommended fix is changing npm's default global directory to a user-writable location
- Alternatively, Node version managers (nvm, fnm) install Node in the home directory, avoiding this entirely
- This issue does not affect project-level (local) npm install which uses node_modules/ in the project
Common Causes
- Default npm global directory (/usr/local/) requires root ownership
- Previous sudo npm install changed file ownership to root in node_modules
- Node installed via system package manager (apt, yum) rather than nvm or direct download
- EACCES error on ~/.npm cache directory after mixed root and user npm usage
Steps
- 1Create a new global directory: 'mkdir ~/.npm-global'
- 2Configure npm to use it: 'npm config set prefix ~/.npm-global'
- 3Add to PATH in ~/.bashrc: 'export PATH=~/.npm-global/bin:$PATH' then 'source ~/.bashrc'
- 4Better alternative: install nvm and use it to manage Node — eliminates all permission issues
- 5Fix cache permissions if needed: 'sudo chown -R $(whoami) ~/.npm'
Tags
npmeaccespermissionglobal-installnode
Related Items
More in Package
Frequently Asked Questions
Never use sudo with npm. It creates files owned by root in your home directory, leading to cascading permission problems. Fix the underlying issue by changing the global directory or using nvm.