npm ERR! code EACCES — How to Fix
npm EACCES errors happen when npm tries to write to directories your user doesn't own. This guide covers every fix, from reconfiguring npm to reinstalling Node.js.
What You'll Learn
How to fix npm permission errors permanently by reconfiguring npm's default directory, without running npm with sudo.
Why It Matters
Using sudo npm creates security risks and breaks permission models — every developer should fix this once and never see it again.
Real-World Use
You run npm install -g create-react-app and get EACCES: permission denied, mkdir '/usr/lib/node_modules'.
Step 1 — Reconfigure npm's Global Directory (Recommended)
The safest fix is to tell npm to use a directory your user owns:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
Add the directory to your PATH by adding this to ~/.zshrc, ~/.bashrc, or ~/.profile:
export PATH=~/.npm-global/bin:$PATH
Reload your shell:
source ~/.zshrc
Expected behavior: npm install -g <package> now installs to ~/.npm-global/lib/node_modules without requiring sudo.
Step 2 — Fix npm Cache Permissions
If errors persist, clear and fix the npm cache:
npm cache clean --force
sudo chown -R $(whoami) ~/.npm
Expected behavior: The npm cache is now owned by your user and can be written to without elevated permissions.
Alternative Fixes
Option A — Use a Version Manager (Best Practice)
Install nvm (Node Version Manager):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
Then install Node.js through nvm:
nvm install --lts
Expected behavior: nvm manages all Node.js and npm installations in your home directory — no permission issues ever.
Option B — Change npm's Directory Ownership
Only do this if you're the only user on the system:
sudo chown -R $(whoami) /usr/local/lib/node_modules
sudo chown -R $(whoami) /usr/local/bin
sudo chown -R $(whoami) /usr/local/share
Expected behavior: npm can now write to the default global directories.
Prevention Tips
- Install nvm immediately after setting up a new machine
- Never run
sudo npm— it creates files owned by root - Use
npxinstead of global installs where possible - Regularly update npm with
npm install -g npm@latest
Quick Reference
| Approach | Command | Best For |
|---|---|---|
| Reconfigure prefix | npm config set prefix ~/.npm-global |
All users |
| Use nvm | curl install nvm && nvm install --lts |
New setups |
| Change ownership | sudo chown -R $(whoami) /usr/local/lib |
Single-user systems |
| Never use sudo | Just don't run sudo npm |
Everyone |
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro