npm install -g fails with EACCES on /usr/local/lib/node_modules
npm error code EACCESShort answer
EACCES means npm tried to write global packages into a folder your user does not own, usually /usr/local/lib/node_modules. Do not fix it with sudo or chmod on system folders. Point npm's prefix at a folder in your home directory, add its bin folder to PATH, and install again.
Fix it fast
1Find out which cause you have
Find out where global installs go and who owns that folder. If the owner is root and you are not, you have found the problem.
npm config get prefix ls -ld "$(npm config get prefix)/lib/node_modules"2Match what you see, then jump to its fix
What does the error look like?
npm error code EACCESnpm error syscall accessnpm error path /usr/local/lib/node_modulesError: EACCES: permission denied, access '/usr/local/lib/node_modules'
Cause 1 of 2
The global prefix is a system folder owned by root
1Check
By default npm's prefix is where Node.js is installed, often /usr/local. A Node.js installed by a system package or an installer leaves that tree owned by root.
npm config get prefix2Fix
Give npm a prefix inside your home folder and put its bin folder on PATH. Then global installs never need elevated rights. On Windows this method does not apply.
npm config set prefix ~/.local
echo 'PATH=~/.local/bin:$PATH' >> ~/.profile
source ~/.profile3Verify
Install a global package without sudo and run it.
npm install -g npm-check-updates
ncu --versionConfirmed by docs.npmjs.com
Cause 2 of 2
You use zsh and the new PATH line is never read
1Check
zsh login shells read ~/.zprofile, not ~/.profile. Check your shell and whether the new folder is on PATH.
echo $0
echo $PATH | tr ':' '\n' | grep local2Fix
Load the profile from the zsh profile file, then reload it.
echo 'source ~/.profile' >> ~/.zprofile
source ~/.profile3Verify
The folder shows up in PATH in a new terminal.
echo $PATH | tr ':' '\n' | grep localConfirmed by docs.npmjs.com
How do I stop it coming back?
- Never use sudo with npm install; a root-owned file will cause the next EACCES.
- Install Node.js through a version manager or an installer that keeps its files in your home folder.
- For one-off tools, run them with npx instead of installing them globally.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fixBack end
corepack enable fails: permission denied while creating the pnpm link
Internal Error: EACCES: permission denied, symlink '../lib/node_modules/corepack/dist/pnpm.js' -> '/usr/local/bin/pnpm'
corepack enable writes small link files for yarn and pnpm next to your node binary.
2 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
firebase deploy cannot load your functions: user code failed to load
Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
The Firebase CLI loads your functions code on your machine before it deploys, to learn which functions exist.
4 causes, each with a fixBack end
firebase deploy stops because the predeploy lint step failed
Error: There was an error deploying functions
The predeploy hook in firebase.json runs npm run lint before every deploy, and a failing hook cancels the deploy.
3 causes, each with a fixBack end
fluent-ffmpeg cannot find the ffmpeg or ffprobe binary
Error: Cannot find ffprobe
fluent-ffmpeg does not bundle ffmpeg.
4 causes, each with a fix