Skip to main content

npm · 4 common causes

npm EACCES: permission denied

Updated

npm tried to write into a directory your user does not own — almost always a global install into a system-owned Node.

The exact message

npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/typescript
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/typescript'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/typescript'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir'
npm ERR! }

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is npm EACCES: permission denied.

How to fix it

Cause 1 of 4

Node was installed system-wide, so its global directory belongs to root.

Install Node through a version manager that keeps everything under your home directory — nvm, fnm, Volta or asdf. After nvm install --lts, global installs need no elevated permissions at all. This is the fix npm's own documentation recommends, and it also lets you switch Node versions per project.

Cause 2 of 4

You want to keep the system Node but stop the permission errors.

Point npm's global prefix at a directory you own: mkdir ~/.npm-global then npm config set prefix ~/.npm-global, and add ~/.npm-global/bin to PATH in your shell profile. New global installs land there and need no sudo.

Cause 3 of 4

The package does not need to be global at all.

Run it once with npx <tool>, or add it to the project's devDependencies and call it from an npm script. Project-local tools also pin the version per repository, which a global install cannot.

Cause 4 of 4

An earlier sudo install left root-owned files in your cache or project.

Give the cache back to your user: sudo chown -R $(id -u):$(id -g) ~/.npm. Check the exact path printed in the error before running it, and never widen permissions with chmod 777 — that lets any account on the machine modify code you are about to execute.

Stopping it coming back

Never run sudo npm install -g — it is the cause of most later EACCES errors, not the cure, because it seeds root-owned files into directories npm expects to own. A version manager removes the problem permanently.

npm EACCES: permission denied — common questions

Why is sudo npm install -g the wrong answer?
It works once and creates the next failure. Packages run install scripts, so sudo hands arbitrary package code root on your machine, and the files it writes into ~/.npm and node_modules are then owned by root, which makes your next ordinary install fail. A version manager avoids both problems.
Is EACCES ever about a project directory rather than a global install?
Yes — if the error path is inside your project, something created files there as another user, usually a container bind mount or an earlier sudo. ls -la shows the owner; chown the tree back to your user rather than loosening the mode.
Does this happen on Windows?
Rarely in this form. Windows produces EPERM instead, most often because a file is locked by a running process or an antivirus scanner. Close the process holding it and retry before changing any permissions.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor