The EACCES permission denied error occurs when npm lacks write access to global package directories. Fix it by using nvm to manage Node.js, changing npm's default directory, or fixing directory ownership. Never use sudo with npm.
This error occurs when npm cannot write to the directories it needs for installing packages—typically the global node_modules directory at /usr/local/lib/node_modules. On Unix-like systems, this directory is usually owned by root, but npm runs as your regular user. The error most commonly appears when running `npm install -g` to install global packages. npm tries to write to /usr/local but your user doesn't have write permission there. While using `sudo npm install` might seem like a quick fix, it creates files owned by root, which causes more permission problems later. The proper solution is to configure npm to use directories your user can write to.
The best solution is to install Node.js via nvm (Node Version Manager), which avoids permission issues entirely:
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Restart your terminal, then install Node.js
nvm install 20
nvm use 20
# Verify
node --version
npm --versionnvm installs Node.js in your home directory, so no sudo is ever needed.
Configure npm to use a directory in your home folder:
# Create directory for global packages
mkdir -p ~/.npm-global
# Configure npm to use it
npm config set prefix '~/.npm-global'
# Add to your shell profile (~/.bashrc, ~/.zshrc, etc.)
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
# Reload your shell
source ~/.bashrcNow global packages install to ~/.npm-global without needing elevated permissions.
If you want to keep using /usr/local, change its ownership:
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}Warning: Only do this if npm's prefix is /usr/local. Never chown /usr or /usr/lib directly—that breaks your system.
First check your prefix:
npm config get prefixIf the error specifically mentions the cache directory:
sudo chown -R $(whoami) ~/.npmThis gives your user ownership of the npm cache, which is always in your home directory.
Sometimes corrupted cache causes permission errors:
npm cache clean --force
npm install -g <package-name>On Windows, configure npm to use a folder in your user directory:
# Create directory
mkdir "$env:USERPROFILE\.npm-global"
# Set npm prefix
npm config set prefix "$env:USERPROFILE\.npm-global"
# Add to PATH (System Properties > Environment Variables)
# Add: %USERPROFILE%\.npm-globalRestart your terminal for PATH changes to take effect.
Why you should never use sudo with npm:
Running sudo npm install creates files owned by root. The next time you run npm without sudo, it can't modify those files, leading to more EACCES errors. This creates a cycle where you need sudo for everything.
Worse, npm runs arbitrary code from packages during installation. Running that code as root is a security risk—a malicious package could modify system files.
For Docker containers:
If you need npm in Docker, either:
1. Run as a non-root user: USER node after FROM node:20
2. Or use official Node.js images which handle permissions correctly
For CI/CD pipelines:
Most CI environments run as non-root users with properly configured npm. If you see EACCES in CI, check:
- Whether a previous step ran something as root
- That the workspace directory has correct permissions
- That you're not mixing sudo and non-sudo npm commands
npm ERR! code ENOAUDIT npm ERR! Audit endpoint not supported
How to fix "npm ERR! code ENOAUDIT - Audit endpoint not supported"
npm ERR! code EBADDEVENGINES npm ERR! devEngines.runtime incompatible with current node version
How to fix "npm ERR! code EBADDEVENGINES - devEngines.runtime incompatible with current node version"
npm ERR! code ETOOMANYARGS npm ERR! Too many arguments
How to fix "npm ERR! code ETOOMANYARGS - Too many arguments"
npm ERR! code EINVALIDTAGNAME npm ERR! Invalid tag name: tag names cannot contain spaces
How to fix "npm ERR! code EINVALIDTAGNAME - tag names cannot contain spaces"
npm ERR! code E400 npm ERR! 400 Bad Request
How to fix "npm ERR! code E400 - 400 Bad Request" error