Lorixen.
EN

npm install -g says EACCES permission denied? Skip sudo

Short answer: EACCES: permission denied during npm install -g means npm is trying to write global packages into a folder your user doesn't own, usually one created by the system's Node installer. Don't fix it with sudo: running npm as root creates root-owned files that cause more permission errors later and runs package install scripts with full system access. npm's own documentation recommends reinstalling Node and npm with a Node version manager, or pointing npm's global folder to a directory in your home folder. For tools you only use occasionally, npx avoids global installs entirely.

Why the error happens

When you install a package globally, npm puts it under its "prefix" directory, so the command is available everywhere on that machine. Where that directory is depends on how Node was installed. npm's installation guide says it doesn't recommend using a Node installer, because the installation puts npm in a directory with local permissions that can cause permission errors when you install packages globally. That's the classic cause of EACCES on a fresh laptop or server.

The error appears as soon as npm tries to create a folder or file there as your normal user. To see where npm is trying to write, run npm config get prefix; if the answer is a system folder such as /usr or /usr/local that belongs to root, you've found the cause. The full error message also names the exact path npm couldn't write to.

Why sudo is the wrong fix

sudo npm install -g some-tool usually "works", which is why it's so common in old setup guides. The problems come later:

  • Files created by root end up in folders your user also uses, so later commands fail with the same error in a different place, including npm's own cache.
  • Package install scripts run as root, giving any compromised or badly written package full control of the machine.
  • On a shared server, everyone depends on whatever root installed, at whatever version.

If you've already used sudo, you may need to fix ownership of npm's cache and global folders before the clean approaches below work.

npm's documentation calls this the best way to avoid permission issues. A version manager installs Node and npm inside your home directory, so global packages go there too and never need root. It also lets you switch Node versions per project, which helps when one app needs an older release.

npm notes you don't need to remove your existing Node.js or npm before installing a version manager. After installing, open a new terminal, install the Node version you want, and confirm which node and which npm point into your home directory.

Fix 2: change npm's global directory

If you'd rather keep the existing Node installation, npm documents moving the global prefix to a folder you own. Its example:

  1. Run npm config set prefix ~/.local.
  2. Add PATH=~/.local/bin:$PATH to ~/.profile, and, if you use zsh, source ~/.profile from ~/.zprofile.
  3. Reload with source ~/.profile.
  4. Test with a global install, without sudo.

npm's documentation notes this method doesn't apply to Windows. On Windows, permission errors usually come from antivirus software, a locked folder or an old installation, and the version manager route is generally simpler.

Fix 3: avoid global installs

Many tools don't need to be global at all:

  • Use npx to run a command once, such as a project scaffolding tool. npm suggests it as an alternative to global installs, especially for commands you only need occasionally.
  • Install tools per project as dev dependencies, and run them through npm run scripts. Everyone on the team then uses the same version, recorded in the lock file.

For build tools, linters and formatters, project-local installs are almost always better than global ones.

On servers and in CI

On a server, run Node apps and scheduled jobs as a dedicated non-root user, and install Node for that user with a version manager or your platform's runtime setting. Avoid global packages for anything the app needs at runtime; put them in the app's dependencies so a fresh deploy installs them.

In CI, use your platform's Node setup step and project-local tools. If a job runs as root inside a container, it may not see EACCES at all, which hides the problem until someone runs the same steps on a normal machine.

Cleaning up after sudo

If you previously installed with sudo, a tidy-up usually involves:

  1. listing global packages with npm ls -g --depth=0 so you know what to reinstall,
  2. fixing ownership of your user's npm cache folder if commands complain about it,
  3. switching to a version manager or a user-owned prefix,
  4. reinstalling the global tools you actually need, without sudo.

Remove old global packages you no longer use; fewer global tools means fewer surprises.

An illustrative example

A small IT consultancy in Calgary sets up a new build server for client websites. The setup notes say sudo npm install -g for a deployment CLI, and two weeks later builds fail with EACCES in the npm cache. The team creates a dedicated build user, installs Node for it with a version manager, adds the deployment CLI as a dev dependency in each project, and runs it through npm run deploy. The sudo instructions are removed from the notes.

FAQ

Is chmod 777 on the npm folder a quick fix?

It removes the error by letting anyone write there, which is a security problem. Use the documented fixes instead.

Do I need global packages at all?

Rarely. Most tools work better as project dependencies or through npx.

Will a version manager slow down my terminal?

Some add a small delay when a shell starts. The trade-off is usually worth it for clean permissions and easy version switching.

Does this affect Yarn or pnpm?

They have their own global folders and similar issues. Prefer project-local installs there too.

If your team's machines or build servers are tangled with npm permission problems, tell us how Node is installed today.

Sources

WhatsAppBook a call