npm ERR! code EACCES
The npm ERR! code EACCES error means npm lacks permission to write to the global directory. Fix it by setting a user-owned prefix or switching to nvm.
What It Means
EACCES (Error ACCESs) is the permission-denied error. When you run npm install -g <package>, npm tries to write to a system directory owned by root (e.g., /usr/local/lib/node_modules), and your current user doesn’t have write permission there. The error is often accompanied by: Error: EACCES: permission denied, access '/usr/local/lib/node_modules'.
Why It Happens
- You ran
npm install -gwithoutsudoand the global npm prefix directory is owned byroot. - The
node_modulesor.npmcache directory in your home folder has incorrect permissions. - You’re running npm in a restricted environment (CI, container, or shared hosting).
- A previous
sudo npmcommand created files owned byrootthat npm now can’t overwrite.
How to Fix It
1. Configure a custom global directory (recommended)
mkdir -p "$HOME/.npm-global"
npm config set prefix "$HOME/.npm-global"Add the directory to your PATH by adding this line to ~/.bashrc or ~/.zshrc:
export PATH="$HOME/.npm-global/bin:$PATH"Reload your shell configuration:
source ~/.bashrcNow npm install -g will write to your user directory without needing sudo.
2. Switch to nvm (prevents all permission issues)
nvm install --lts
nvm use --ltsnvm installs Node.js in your home directory by default, eliminating EACCES errors for both global and local installations.
3. Fix permissions on the existing npm prefix (use sudo only once)
sudo chown -R $(whoami) "$(npm config get prefix)/{lib/node_modules,bin,share}"This changes ownership of npm’s global directories to your user. After this, you can run npm install -g without sudo.
4. Fix the npm cache directory
sudo chown -R $(whoami) ~/.npmThis prevents permission errors when npm reads or writes its cache during installations.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro