GrepBoard/Manuals
605 manuals ·

Fix npm install -g EACCES: permission denied without sudo

·Tested on Node.js 22.23.4 · npm 10.9.9 (Docker node:22-slim) · 2026-10·Test scope ↓·6 steps

npm install -g fails with EACCES permission denied when the global folder is owned by root. Move npm's prefix to ~/.local and fix PATH instead of using sudo.

output
npm error code EACCESnpm error syscall mkdirnpm error path /usr/local/lib/node_modules/semvernpm error errno -13npm error Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/semver'

EACCES on npm install -g means your account cannot write to the folder where npm puts global packages. With a system-installed Node.js that folder is usually owned by root. Instead of adding sudo, move the global folder (the prefix) under your home directory and the error stops.

[1] Check where the npm error path points. If it is under npm prefix -g and owned by root, this guide applies.

console
$ npm prefix -g$ ls -ld /usr/local/lib/node_modules
output
/usr/localdrwxr-xr-x 4 root root 33 Oct 20 19:57 /usr/local/lib/node_modules

If the path is inside a project (./node_modules) or under ~/.npm, the global folder is not the problem and [3] below does not apply. Check that path's owner and permissions first with ls -ld <path>. Files left root-owned by an earlier sudo npm … are one common cause, but a folder created by another account, permission bits or a read-only mount are also possible, so the path alone does not tell you which.

[2] Decide whether you will use a Node version manager such as nvm. The npm docs call reinstalling Node.js with a version manager the best way to avoid these errors: it installs Node.js under your home, so the global folder is yours from the start. If you plan to use nvm, skip step [3]. The nvm README says a prefix setting in ~/.npmrc is not compatible with nvm.

[3] Without a version manager, move npm's global folder under your home. The current npm docs use ~/.local.

console
$ npm config set prefix ~/.local$ npm config get prefix$ cat ~/.npmrc
output
/home/dev/.localprefix=/home/dev/.local

The setting is a single line in your user ~/.npmrc. From now on npm install -g puts packages in ~/.local/lib/node_modules and executables in ~/.local/bin, and leaves the root-owned folder alone.

[4] Install again and see where the executable went.

console
$ npm install -g semver$ ls ~/.local/bin
output
added 1 package in 749mssemver

[5] Put ~/.local/bin on your PATH. This is where "installed but command not found" comes from. The default ~/.profile on Debian and Ubuntu already has these lines:

console
$ grep -n -A2 .local/bin ~/.profile
output
25:if [ -d "$HOME/.local/bin" ] ; then26:    PATH="$HOME/.local/bin:$PATH"27-fi

The directory is added only if it exists, so if ~/.local/bin did not exist when you logged in, your current shell does not have it. Looking for the command right after the install, in the same shell:

console
$ npm config set prefix ~/.local; npm install -g semver >/dev/null; command -v semver || echo "semver: not on PATH"; . ~/.profile; command -v semver
output
semver: not on PATH/home/dev/.local/bin/semver

Re-read it with . ~/.profile or log in again. On a distribution without those lines, add PATH=~/.local/bin:$PATH to ~/.profile as the npm docs say, and with zsh add source ~/.profile to ~/.zprofile.

[6] Find out where your old global packages are. After the prefix move, npm ls -g shows only the new folder.

console
$ npm ls -g
output
/home/dev/.local/lib`-- semver@7.8.5 

Look at the old folder with --prefix:

console
$ npm config set prefix ~/.local; npm ls -g --prefix /usr/local
output
/usr/local/lib+-- corepack@0.36.0`-- npm@10.9.9

npm itself lives in the old folder. If packages you installed earlier with sudo show up here, leave them and reinstall them under the new prefix. When the same command exists in both places, whichever comes first on PATH runs, so check with command -v <command>.

Undoing it is one line. Deleting the setting puts the global folder back.

console
$ npm config delete prefix; npm prefix -g
output
/usr/local

If an npm-installed CLI such as Claude Code fails to auto-update for the same reason, see Claude Code auto-update failed. Dependency conflicts (ERESOLVE) are covered in npm ERR! code ERESOLVE. If npm cannot find package.json at all (ENOENT), see Fix npm ENOENT: Could not read package.json; if a native module build fails with gyp ERR! find Python, see Fix gyp ERR! find Python.

Check it with the chmod · umask calculator tool ↗ — calculated in your browser.

#npm#nodejs#eacces#permissions