Skip to content
npm ERR! code EACCES

npm ERR! code EACCES

DodaTech 2 min read

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 -g without sudo and the global npm prefix directory is owned by root.
  • The node_modules or .npm cache directory in your home folder has incorrect permissions.
  • You’re running npm in a restricted environment (CI, container, or shared hosting).
  • A previous sudo npm command created files owned by root that 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 ~/.bashrc

Now 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 --lts

nvm 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) ~/.npm

This prevents permission errors when npm reads or writes its cache during installations.

Is it safe to use sudo npm install -g?
It works but is not recommended. Packages installed with sudo run with elevated privileges, and if a package has a malicious postinstall script, it can execute arbitrary code as root. Always prefer configuring a user-owned prefix or using nvm.
Will this fix EACCES for local (non-global) installs?
Local installs write to node_modules inside your project directory, which your user should already own. If you see EACCES during npm install (without -g), check the ownership of your project folder and node_modules — a previous sudo npm install may have created files owned by root.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro