How to fix npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/<package>' in npm

npmBEGINNERMEDIUMUpdated Dec 2, 2025ยท 348 views

npm cannot create the global package directory because the prefix is unwritable or owned by root. Use nvm/user prefixes or fix ownership, clear staging, then reinstall.

Quick fix

Fixes npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/<package>'

npm config get prefix
ls -ld $(npm config get prefix)/lib/node_modules
sudo ls -ld $(npm config get prefix)/lib/node_modules/.staging 2>/dev/null || true
  1. Check prefix and staging ownership
  2. Use nvm/fnm/asdf or set a user prefix
  3. Clean temp directories and cache
  4. Realign ownership once if you control the host
npm โ€” error
npm config get prefix
ls -ld $(npm config get prefix)/lib/node_modules
sudo ls -ld $(npm config get prefix)/lib/node_modules/.staging 2>/dev/null || true
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/<package>'

What this error means

Global installs create a package directory under prefix/lib/node_modules before copying files. If that parent directory is owned by root or mounted read-only, npm's mkdir syscall fails with EACCES and the install stops. Temporary .staging directories from past sudo installs can also remain root-owned, preventing new directories from being created. This shows up on macOS Homebrew installs, Linux servers with hardened /usr/local permissions, Docker/CI jobs running as non-root, and Windows/WSL when corporate AV interferes with mkdir inside node_modules.

Typical symptoms

  • npm install -g fails while creating /usr/local/lib/node_modules/<package>
  • Works with sudo/Admin but not as a regular user
  • Leftover .staging dirs remain after failures
  • npm config get prefix reports /usr/local or another protected path
  • CI/WSL installs fail even though local per-project installs succeed
  • Terminal shows EACCES on mkdir plus a permission denied stack trace

Common causes

  1. Root-owned global prefix from system or package-manager Node installs
  2. Stale root-owned .staging directories from prior sudo npm commands
  3. Read-only mounts or corporate lockdown of /usr/local
  4. Docker/CI running npm as a non-root user against a root-owned layer
  5. Homebrew prefix ownership drift after OS or Xcode updates
  6. Windows/WSL antivirus locking mkdir operations in node_modules

How to fix "npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/<package>'"

1Check prefix and staging ownership
bash
npm config get prefix
ls -ld $(npm config get prefix)/lib/node_modules
sudo ls -ld $(npm config get prefix)/lib/node_modules/.staging 2>/dev/null || true
2Use nvm/fnm/asdf or set a user prefix
bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \ . "$NVM_DIR/nvm.sh"
nvm install --lts
# or
mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
export PATH="$HOME/.npm-global/bin:$PATH"
3Clean temp directories and cache
bash
sudo rm -rf $(npm config get prefix)/lib/node_modules/.staging 2>/dev/null || true
npm cache clean --force
4Realign ownership once if you control the host
bash
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
5Retry the global install without sudo
bash
npm install -g <package>
6Handle Windows/WSL locks

Close editors holding node_modules, add Defender exclusions for your prefix, and retry from an elevated shell if required by policy.

Advanced notes

On macOS with Homebrew, ensure /usr/local or /opt/homebrew remains owned by the brew user; reinstalling Node via nvm/fnm keeps globals under $HOME and avoids permission drift. In Docker/CI, set npm config prefix to a workspace-owned path or run as the image owner to avoid EACCES during mkdir. Corporate AV/EDR on Windows/WSL can lock mkdir/unlink; exclusions for node_modules and temp folders prevent recurring failures.

Frequently asked questions

What does "npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_" mean?
Global installs create a package directory under prefix/lib/node_modules before copying files. If that parent directory is owned by root or mounted read-only, npm's mkdir syscall fails with EACCES and the install stops. staging directories from past sudo installs can also remain root-owned, preventing new directories from being created.
How do I fix "npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_"?
npm cannot create the global package directory because the prefix is unwritable or owned by root. Use nvm/user prefixes or fix ownership, clear staging, then reinstall.
What causes "npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_"?
Root-owned global prefix from system or package-manager Node installs Stale root-owned .staging directories from prior sudo npm commands Read-only mounts or corporate lockdown of /usr/local
Is the "npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading