Fix EACCES Permission Errors During npm Install in Claude Code
The Problem
Claude Code runs npm install and hits a permission error:
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/some-package
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied
Quick Fix
For local project installs, fix the node_modules ownership:
sudo chown -R $(whoami) node_modules/
npm install
For global install permission issues, configure npm to use a user-owned directory:
mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
What Is Happening
EACCES errors occur when the current user lacks write permission to a directory that npm needs to modify. This happens in three common scenarios:
- Previous sudo install: Someone ran
sudo npm installand now root owns files insidenode_modules/. - Global prefix pointing to system directory: npm's default global prefix (
/usr/local) requires root access. - Corrupted npm cache: The npm cache directory has mixed ownership from previous sudo operations.
Step-by-Step Fix
Step 1: Diagnose the exact cause
npm install 2>&1
ls -la node_modules/ | head -20
Step 2: Fix node_modules ownership
# Check for root-owned files
find node_modules/ -user root -type f | head -10
# Fix ownership recursively
sudo chown -R $(whoami):$(id -gn) node_modules/
# Now install normally
npm install
Step 3: Fix global prefix
mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
npm config get prefix
# Should output: /Users/you/.npm-global
Step 4: Fix npm cache permissions
sudo chown -R $(whoami):$(id -gn) ~/.npm
# Or clean the cache entirely
npm cache clean --force
Using pnpm Instead
pnpm avoids most EACCES issues because it uses a content-addressable store in user space:
npm install -g pnpm
pnpm install
Prevention
Never run sudo npm install for local project dependencies. If a tutorial tells you to use sudo with npm, the correct fix is always to change ownership or the global prefix. Add a .npmrc file to your project root for consistent behavior:
engine-strict=true
save-exact=true
fund=false
audit=false
Configure permissions with our CLAUDE.md Generator to prevent permission issues.
Master Claude Code
Get lifetime access to all ClaudHQ tools, advanced workflows, and production-grade templates.
Get Lifetime AccessWritten by the ClaudHQ team ยท Expert Claude Code guides and tools