npm install -g EACCES Permission Error: 2 Fixes Without sudo (2026)
When npm install -g fails with EACCES: permission denied, npm is trying to write into a directory your current user can't write to. That is usually the global directory of a system-installed Node (for example /usr/lib/node_modules). The right fix is not to add sudo. Make the global directory yours instead: install Node with a version manager such as nvm, or point npm's prefix at a directory under your home folder.
The examples below use command-line tools like Claude Code and Codex. First confirm the cause, then pick one of two fixes you can follow as written.
What does the error look like, and what does it mean?
Typical output:
npm error code EACCES
npm error syscall mkdir
npm error path /usr/lib/node_modules/@anthropic-ai
npm error errno -13
npm error Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/@anthropic-ai'
The path line is the one that matters. It names the directory npm wanted to write to and couldn't. If the path is under a system directory such as /usr or /usr/local, your Node was installed with a system package manager (apt, dnf, and so on) and the global directory belongs to root. If the path is under ~/.npm in your home directory, that is a different case, covered below.
Why not just use sudo?
sudo npm install -g will make the command succeed, but it causes real problems:
- Package install scripts (postinstall and the like) run as root. You are handing the whole machine to that package and all of its dependencies.
- Root-owned files then show up in the npm cache or global directory. The next time you run without sudo you get EACCES again, and each patch makes the mess worse.
- The official npm documentation on resolving EACCES also recommends a version manager or changing the prefix rather than elevating privileges. Defer to the official npm docs for details.
Fix 1: Install Node with nvm (preferred)
nvm installs Node under your home directory, so global packages land there too. That removes the permission problem at the root.
Check which Node you're using now:
which node npm config get prefixIf the output is
/usr/bin/nodeor/usr/local, you're on a system directory.Install nvm following the instructions on the project page (github.com/nvm-sh/nvm). The install script's version number changes over time, so copy the latest command from that page rather than one from an old article.
Open a new terminal, then install and activate the LTS release of Node:
nvm install --lts nvm use --ltsConfirm the paths now point into your home directory (usually under
~/.nvm):which node npm config get prefixReinstall without sudo, for example:
npm install -g @anthropic-ai/claude-code npm install -g @openai/codexPackage names are as commonly written at the time of writing; check each tool's official docs.
On Windows you can use nvm-windows or the official installer. Windows more often runs into EPERM or antivirus locking files, which has a different cause and is outside the scope of this article.
Fix 2: Keep your Node, change npm's global directory
If you'd rather not touch your existing Node, tell npm to install global packages into a directory you own:
mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'
Then add its bin directory to your PATH. Put this in ~/.bashrc for bash or ~/.zshrc for zsh:
export PATH=$HOME/.npm-global/bin:$PATH
Run source ~/.bashrc (or source ~/.zshrc), then install:
npm install -g @anthropic-ai/claude-code
Afterward, run which claude and check that the command lives under ~/.npm-global/bin. If you get "command not found", it is almost always because PATH hasn't taken effect. Open a new terminal and try again.
What if the error path is under ~/.npm?
If path points to something like /home/yourname/.npm/_cacache, an earlier sudo install changed the owner of the cache files to root. Fix the ownership:
sudo chown -R "$(id -u):$(id -g)" ~/.npm
Note that sudo is used here only to change file ownership, not to install packages. This works on both macOS and Linux. Before running it, make sure the path is your own home directory.
Quick triage table
| Symptom | Likely cause | Fix |
|---|---|---|
| path is under /usr/lib or /usr/local | Global directory belongs to root | Fix 1 or Fix 2 |
| path is under ~/.npm | Cache was written by root | chown to repair ownership |
Install succeeds but claude is not found | PATH doesn't include the global bin | Check npm config get prefix and add its bin to PATH |
| Command disappears in a new terminal | nvm default version not set | nvm alias default lts/* |
| EACCES with a path inside a project directory | Wrong owner on the project directory | Check directory permissions; don't install inside a root-owned directory |
Installed, but still can't connect?
Once permissions are sorted, the next class of problems is usually the network: timeouts after the command starts, certificate errors, or streaming output that gets cut off. These articles walk through each:
- Claude Code connection timeouts and errors: a four-layer network checklist
- Claude Code proxy settings not working: step-by-step checks and verification commands
- Codex "stream disconnected before completion": how to troubleshoot
- curl SSL_ERROR_SYSCALL: a checklist for failed TLS handshakes
If you suspect the service itself, check /en/down/claude-code and /en/status/anthropic. They combine the official status pages with cdnping's own API probes. To see how your current egress network looks, use /en/ip for an environment check.
Tools installed, but no overseas account?
npm only puts the command-line tool on your machine. To use it you still need an endpoint and an API key. Claude Code and Codex connect to their vendors' official services by default, which requires the matching account and a supported region.
If you don't have an overseas credit card, or you want one subscription that lets you switch between models, you could look at AGI Plan (AGI Plan is a commercial partner of cdnping). It is a monthly-subscription, RMB-billed gateway that aggregates LLMs from multiple providers and doesn't require an overseas credit card. Claude Code and Codex only need one endpoint address changed. Setup is roughly three steps: sign up with an emailed verification code, pick a plan and create a key, and change one setting. Some models are marked as available only outside China; see https://agiplan.dev/models for specifics. The site states clearly that it is not an officially authorized channel. Read its description before deciding whether it suits you, and follow each service's terms and local regulations.
Next steps
- Run
which nodeandnpm config get prefixto confirm whether the global directory is a system directory. - If it is, choose Fix 1 (nvm) or Fix 2 (change the prefix), then reinstall without sudo.
- If the command installs but connections fail, work through the troubleshooting articles above layer by layer.
- To learn how to set up AGI Plan, see the AGI Plan beginner guide.