npm EACCES Permission Error — Global Package Install Permission Denied
About npm EACCES Permission Error
Fix npm EACCES error when installing global packages fails because the default global directory requires root permissions that npm should not use. This guide covers everything you need to know about this topic, including common causes, step-by-step solutions, and answers to frequently asked questions.
Here are the key things to understand: 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. Understanding these fundamentals will help you diagnose and resolve this issue more effectively.
The most common reasons this occurs include: 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. Identifying the root cause is the first step toward finding the right solution.
To resolve this, follow these recommended steps: Create a new global directory: 'mkdir ~/.npm-global'. Configure npm to use it: 'npm config set prefix ~/.npm-global'. Add to PATH in ~/.bashrc: 'export PATH=~/.npm-global/bin:$PATH' then 'source ~/.bashrc'. Better alternative: install nvm and use it to manage Node — eliminates all permission issues. Fix cache permissions if needed: 'sudo chown -R $(whoami) ~/.npm'. If these steps do not resolve the issue, consider consulting additional resources or a qualified professional.
This article is part of our Linux Error Codes collection on Error Codes Wiki. We provide comprehensive, up-to-date information to help you find solutions quickly.
Quick Answer
Should I use sudo with npm?
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.
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'