npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
What causes this
npm is trying to write to a directory your user doesn’t own. This happens when:
- Node.js was installed with
sudoor from a system package manager, so global packages go to/usr/local/lib/node_modules(owned by root) - Someone ran
sudo npm install -gat some point, which changed ownership of npm directories - You’re trying to install a global package without the right permissions
The fix is NOT to use sudo npm install. That makes the problem worse by creating more root-owned files in your npm directories.
Fix 1: Use nvm (recommended)
The best long-term fix. nvm installs Node.js in your home directory, so you never need sudo:
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# Restart your terminal, then:
nvm install --lts
nvm use --lts
# Verify — should point to ~/.nvm/
which node
which npm
Now npm install -g works without sudo because everything lives in ~/.nvm/.
Fix 2: Change npm’s default directory
If you don’t want to use nvm, tell npm to use a directory you own:
# Create a directory for global packages
mkdir -p ~/.npm-global
# Configure npm to use it
npm config set prefix '~/.npm-global'
# Add to your PATH
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
# or for zsh:
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
# Reload your shell
source ~/.bashrc
Now global installs go to ~/.npm-global/ instead of /usr/local/.
Fix 3: Fix ownership of existing directories
If you just need a quick fix and understand the implications:
# Fix ownership of npm directories
sudo chown -R $(whoami) /usr/local/lib/node_modules
sudo chown -R $(whoami) /usr/local/bin
sudo chown -R $(whoami) /usr/local/share
This works but isn’t ideal — other tools also use /usr/local/ and might have issues.
Fix 4: Fix npm cache permissions
Sometimes the error is specifically about the npm cache:
# Fix cache directory ownership
sudo chown -R $(whoami) ~/.npm
# Or clear and recreate it
rm -rf ~/.npm
npm cache clean --force
Fix 5: Use npx instead of global installs
Many tools don’t need to be installed globally:
# ❌ Global install (needs permissions)
npm install -g create-react-app
create-react-app my-app
# ✅ Use npx (no global install needed)
npx create-react-app my-app
Related resources
How to prevent it
- Use nvm to manage Node.js — it avoids all permission issues by keeping everything in your home directory
- Never run
sudo npm install -g— it creates root-owned files that cause more EACCES errors later - Prefer
npxover global installs for CLI tools you don’t use daily - If you’re setting up a new machine, install Node.js via nvm from the start, not from your system package manager