How to fix npm ERR! code EACCESSDENIED npm ERR! Access denied in npm
Access denied errors in npm typically stem from file permission issues when installing global packages or writing to node_modules. Use nvm for Node.js management, configure a user-writable npm prefix, or fix directory ownership.
Quick fix
Fixes EACCES
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Open new terminal, then:
nvm install --lts
nvm use --lts- Use nvm instead of system Node.js
- Fix local project permissions
- Configure npm to use home directory
- Fix npm cache ownership
# Install nvmcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash# Open new terminal, then:nvm install --ltsnvm use --ltsnpm ERR! code EACCESSDENIEDnpm ERR! Access denied
What this error means
This error indicates npm cannot read from or write to a directory it needs access to. The most common scenario is trying to install global packages without proper permissions to /usr/local/lib/node_modules. On Unix-like systems, the global npm directory is typically owned by root, while npm runs as your user. On Windows, similar issues occur when npm tries to write to protected directories or when antivirus software blocks file operations. The error can also occur with local installations if your project's node_modules directory has incorrect ownership—often from previously running npm with sudo.
Typical symptoms
- EACCES: permission denied during npm install
- Access denied when installing global packages
- Cannot create directory in /usr/local/lib/node_modules
- Local npm install fails with permission errors
- Works with sudo but creates more problems later
- Error occurs on first npm operation after Node.js installation
- Antivirus blocks npm operations on Windows
Common causes
- Global npm directories owned by root
- Previous sudo npm use created root-owned files
- Node.js installed with system package manager as root
- Local node_modules has wrong ownership
- Windows antivirus or security software blocking npm
- VSCode or other editors locking files during installation
- WSL file permission mismatches
How to fix "npm ERR! code EACCESSDENIED npm ERR! Access denied"
The recommended long-term solution is using nvm:
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Open new terminal, then:
nvm install --lts
nvm use --ltsnvm keeps everything in ~/.nvm, avoiding permission issues entirely.
If the error is in your project's node_modules:
# Change ownership to your user
sudo chown -R $(whoami) node_modules
# Or remove and reinstall
rm -rf node_modules package-lock.json
npm installSet up npm to install global packages in your home folder:
npm config set prefix ~/.local
echo 'export PATH=~/.local/bin:$PATH' >> ~/.bashrc
source ~/.bashrcThis avoids needing to modify system directories.
If errors reference the npm cache:
sudo chown -R $(whoami) ~/.npm
npm cache clean --forceOn Windows, access denied often comes from:
1. Antivirus: Temporarily disable real-time scanning and retry
2. Open editors: Close VSCode, WebStorm, or other IDEs that might lock files
3. Terminal: Run Command Prompt or PowerShell as Administrator (for one-time fixes only)
Clear npm cache after:
npm cache clean --forceIn WSL, permission issues are common when working in /mnt/c/:
# Move project to WSL filesystem
mv /mnt/c/project ~/project
cd ~/project
rm -rf node_modules
npm installThe Linux filesystem in WSL handles permissions correctly, unlike mounted Windows drives.
Advanced notes
Why sudo npm is dangerous:
1. Security risk: npm runs package scripts during installation. Running as root means those scripts have root access.
2. Permission cascade: Creates root-owned files that require sudo for all future operations.
3. Breaks user installs: Mixing sudo and non-sudo npm corrupts permissions.
If you've already used sudo npm, the fix is to:
1. Remove node_modules: sudo rm -rf node_modules
2. Fix global directory ownership or switch to nvm
3. Never use sudo with npm again
For CI/CD:
If you see access denied in CI pipelines:
- Ensure the CI user has write access to the workspace
- Don't run npm as root in Docker
- Use npm ci instead of npm install for cleaner installs
- Check that previous pipeline steps didn't change permissions
Frequently asked questions
- What does "EACCES" mean?
- This error indicates npm cannot read from or write to a directory it needs access to. The most common scenario is trying to install global packages without proper permissions to /usr/local/lib/node_modules. On Unix-like systems, the global npm directory is typically owned by root, while npm runs as your user.
- How do I fix "EACCES"?
- Access denied errors in npm typically stem from file permission issues when installing global packages or writing to node_modules. Use nvm for Node.js management, configure a user-writable npm prefix, or fix directory ownership.
- What causes "EACCES"?
- Global npm directories owned by root Previous sudo npm use created root-owned files Node.js installed with system package manager as root
- Is the "EACCES" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.