npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/some-package'
npm is trying to write to a system directory that your user doesn’t own. This happens when you install Node.js via the system package manager and then try npm install -g.
Do NOT fix this with sudo npm install -g. That creates files owned by root and causes more permission problems later.
Fix 1: Use nvm (Best Solution)
nvm (Node Version Manager) installs Node in your home directory, so you never need sudo.
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
# Restart your terminal, then:
nvm install --lts
nvm use --lts
# Verify
which node # should show ~/.nvm/...
which npm # should show ~/.nvm/...
# Now global installs work without sudo
npm install -g typescript
If you already have Node installed via Homebrew or apt, uninstall it first:
# Mac
brew uninstall node
# Ubuntu/Debian
sudo apt remove nodejs
Fix 2: Change npm’s Default Directory
If you don’t want 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 (add to ~/.bashrc or ~/.zshrc)
export PATH=~/.npm-global/bin:$PATH
# Reload shell
source ~/.bashrc # or source ~/.zshrc
# Now global installs work
npm install -g typescript
Fix 3: Fix Ownership (Quick Fix)
If you just want it to work right now:
# Take ownership of the 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 — system updates might reset the permissions.
Why NOT to Use sudo
# ❌ Never do this
sudo npm install -g some-package
Problems with sudo:
- Creates files owned by root in your project
- Future
npm installcommands may fail with permission errors - Security risk — you’re running untrusted package scripts as root
- Breaks when you try to update or uninstall later
Prevention
The best approach: install Node via nvm from the start. Add this to your setup checklist for new machines:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
nvm install --lts
You’ll never see EACCES again.