Fix npm that fails with permission

We'll check the error, fix ownership of npm dirs, clear the cache, and use npx when needed—or tell you when to escalate.

Category
Troubleshooting · Devices & software
Time
10–20 min
Last reviewed
What you'll need
  • Terminal access
  • sudo (to fix ownership)

Step-by-step diagnostic

Step 1 of 8
Show full guide

Steps

Goal: Fix npm permission errors by correcting ownership, clearing the cache, or using npx.

  • Check the error message. EACCES means npm cannot write to a directory—often ~/.npm, the global modules dir, or project node_modules.
  • Good: You see EACCES or “permission denied.” Proceed to Fix ownership.
  • Bad: Different error (ENOENT, network)—see a different guide.

Fix ownership

Goal: Correct ownership of the npm cache and Node install directory.

  • Run sudo chown -R $(whoami) ~/.npm. The npm cache lives in ~/.npm. Wrong ownership often comes from running npm with sudo.
  • If you use nvm: sudo chown -R $(whoami) ~/.nvm. If Node is in /usr/local: sudo chown -R $(whoami) /usr/local/lib/node_modules /usr/local/bin.
  • Retry the npm command. Confirm you should see the command succeed.
  • Bad: Still fails—proceed to Clear cache.

Clear cache

Goal: Remove a corrupted or permission-broken npm cache.

  • Run npm cache clean --force. Retry the original command.
  • Confirm you should see the command succeed or a different error.
  • Bad: Still fails—check npm config get prefix. If it points to /usr/local or another system dir, set a user prefix: npm config set prefix ~/.npm-global and add ~/.npm-global/bin to PATH.

Use npx

Goal: Run packages without global install when you lack write access.

  • Use npx instead of npm install -g: npx <package> runs the package without installing it globally.
  • Example: npx create-react-app my-app. Confirm you should see the package run.
  • Good: npx works for your use case. Bad: You need a persistent global install and cannot fix ownership—escalate.

When to get help

Escalate if:

  • You cannot change ownership (shared system, corporate policy).
  • The error persists after fixing ownership and clearing the cache.
  • You need a global install and npx is not sufficient.

Provide the full error, npm config list, and ls -la of the failing path.

Verification

  • The npm command that failed now runs without EACCES or permission errors.
  • npm install or npm install -g completes successfully.
  • No need to use sudo for npm commands.

Escalation ladder

Work from the device outward. Stop when the problem is fixed.

  1. Fix ownership chown -R on ~/.npm and Node install dir.
  2. Clear cache npm cache clean --force.
  3. Use npx or user prefix npx for one-off runs; set prefix to ~/.npm-global.
  4. Escalate Provide error, npm config list, ls -la of failing path.

What to capture if you need help

Before calling support or posting for help, have these ready. It speeds everything up.

  • Full error message and failing path
  • npm config get prefix and globaldir
  • ls -la of ~/.npm and Node install dir
  • Steps already tried

Does the error mention EACCES or permission denied?

Read the full error. EACCES means npm cannot write to a directory.

Check the error message. Good: EACCES or "permission denied" in the output—proceed to fix ownership. Bad: Different error (ENOENT, network)—different guide.

You can change your answer later.

Can you fix ownership of ~/.npm and the Node dir?

chown -R fixes wrong ownership from sudo npm or mixed installs.

Run `sudo chown -R $(whoami) ~/.npm`. If using nvm: `sudo chown -R $(whoami) ~/.nvm`. If Node in /usr/local: `sudo chown -R $(whoami) /usr/local/lib/node_modules`. Good: ownership fixed—retry npm. Bad: cannot run chown (shared system)—try npx or escalate.

You can change your answer later.

Does npm work after fixing ownership?

Retry the original npm command.

Retry the npm command that failed. Good: command succeeds. Bad: still fails—try npm cache clean --force, then retry.

You can change your answer later.

Clear cache and retry

Run `npm cache clean --force`. Retry the command. If still failing, check npm config get prefix—if it points to a system dir, set prefix to ~/.npm-global.

Can you use npx instead of global install?

npx runs packages without installing them globally.

Use `npx <package>` instead of `npm install -g <package>`. Example: `npx create-react-app my-app`. Good: npx works. Bad: need global install and cannot fix ownership—escalate.

You can change your answer later.

Resolved

npm should work now. Avoid sudo npm in the future.

Escalate

Provide the full error, npm config list, and ls -la of the failing path. Escalate if you cannot fix ownership or the error persists.

Different error

ENOENT, network errors, or other issues need a different guide. Check fix-linux-has-permission-denied for system permission issues.

Reviewed by Blackbox Atlas

Frequently asked questions

Why does npm fail with EACCES?
npm cannot write to its cache (~/.npm), global modules dir, or project node_modules. Wrong ownership, running as root then as user, or a corrupted cache. Fix ownership with chown.
Can I fix npm permission errors myself?
Yes. Run chown on ~/.npm and the Node install dir. Use npm cache clean --force. Use npx instead of global install when you lack write access. Do not use sudo npm.
When should I escalate npm permission issues?
If you cannot change ownership (shared system, corporate policy), or the error persists after fixing ownership and clearing the cache. Provide the full error and npm config list output.

Rate this guide

Was this helpful?

Thanks for your feedback.

Continue to