EACCES
Fix npm ERR! EACCES Permission Denied Error
The EACCES error in npm occurs when the current user lacks write permissions to npm's cache or global installation directories. This guide explains how to resolve it safely.
Symptoms
When running npm commands such as npm install -g or npm cache clean, you encounter an error similar to:
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'This error typically appears on Linux or macOS systems when npm tries to write to directories owned by root, such as /usr/local/lib/node_modules or the npm cache directory (~/.npm).
Root Causes
- Incorrect ownership: The global npm directories are owned by root, but your user account does not have sudo privileges or the correct permissions.
- npm cache corruption: The cache directory (
~/.npm) may have incorrect permissions after a previous sudo-run command. - Misconfigured npm prefix: The npm global prefix (
npm config get prefix) points to a system directory that requires root access. - Missing write permission on project folder: The current project directory may be owned by root or have restrictive permissions.
Step-by-Step Fix
Method 1: Change Ownership of npm Directories (Recommended)
- Find the npm global prefix by running:
npm config get prefix - If the prefix is
/usr/local, change ownership of the npm directories to your user:sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share} - Fix the npm cache directory permissions:
sudo chown -R $(whoami) ~/.npm - Verify the fix by installing a package globally:
npm install -g nodemon
Method 2: Use a Node Version Manager (NVM) – Most Robust
- Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash - Restart your terminal or run:
source ~/.bashrc - Install the latest Node.js:
nvm install node - Now npm is installed in your home directory, avoiding permission issues entirely.
Method 3: Reinstall npm with Correct Permissions
- Uninstall the current npm:
sudo npm uninstall npm -g - Install npm again using Node.js official installer (which sets correct permissions).
Alternative Fixes
- Use sudo temporarily (not recommended):
sudo npm install -g package– but this can cause further permission issues. - Change npm prefix to a user-owned directory:
Then addmkdir ~/.npm-global npm config set prefix '~/.npm-global'export PATH=~/.npm-global/bin:$PATHto your shell profile. - Fix project folder permissions: If the error occurs in a project folder, run
sudo chown -R $(whoami) .inside the project directory.
Prevention
- Avoid using sudo with npm: Never run
sudo npmunless absolutely necessary, as it creates root-owned files. - Use a Node version manager (nvm, n, fnm): This keeps Node.js and npm in your user space, eliminating permission errors.
- Regularly check cache permissions: Run
ls -la ~/.npmto ensure your user owns the cache directory. - Set up a global npm directory in your home folder: As shown in alternative fixes, this is a clean long-term solution.
After applying these fixes, the EACCES error should be resolved. If the problem persists, consider reinstalling Node.js from the official website (nodejs.org) which sets up proper permissions by default.
Was this solution helpful?