More Node.js answers

by Danny
Published 2 min read

npm install -g fails with EACCES when Node was installed into a system folder. Fix it properly with a Node version manager or a user-owned npm prefix, not with sudo.

The short answer

#

npm is trying to write to a folder owned by the system, usually /usr/local/lib/node_modules, and your user cannot write there. Do not reach for sudo. Install Node with a version manager so global packages go into your home folder instead.


_10
# Install nvm (check the nvm README for the current version)
_10
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
_10
_10
# Open a new terminal, then
_10
nvm install --lts
_10
npm install -g typescript

Why it happens

#

The full error looks something like this:


_10
npm error code EACCES
_10
npm error syscall mkdir
_10
npm error path /usr/local/lib/node_modules/typescript
_10
npm error Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/typescript'

Global packages are installed under npm's prefix folder. If you installed Node from the official installer or a Linux package, that prefix is a system location such as /usr/local or /usr, owned by root. A normal npm install -g runs as your user, so it is not allowed to create folders there.

Running sudo npm install -g seems to fix it, but it causes new problems:

  • Package install scripts run as root, which is a real security risk.
  • Files in your npm cache and global folder end up owned by root, so later commands fail with the same error, even without -g.
  • You end up needing sudo for every update.

The npm documentation recommends a Node version manager for exactly this reason.

How to fix it

#

Use a Node version manager

A version manager installs Node inside your home folder, so the global prefix belongs to you. It also lets you switch Node versions per project.

  • nvm on macOS and Linux, shown in the short answer.
  • fnm, a fast alternative that also works on Windows.
  • nvm-windows on Windows.

With fnm:


_10
fnm install --lts
_10
fnm use lts-latest
_10
npm install -g typescript

After switching, check that npm now points at your home folder:


_10
which node
_10
npm config get prefix

Both paths should be inside your home folder. If you previously installed Node system-wide, uninstall that copy so the two do not fight over your PATH.

Or change npm's prefix to a folder you own

If you cannot use a version manager, point npm's global prefix at a folder in your home directory and add its bin folder to your PATH.


_10
mkdir -p ~/.npm-global
_10
npm config set prefix ~/.npm-global
_10
_10
# Add this line to ~/.zshrc or ~/.bashrc
_10
export PATH="$HOME/.npm-global/bin:$PATH"

Open a new terminal and run npm install -g again.

Avoid global installs where you can

Many tools do not need a global install. Add them as dev dependencies so the version is pinned per project, and run them with npx or through package.json scripts.


_10
npm install --save-dev typescript
_10
npx tsc --version
_10
_10
# Run once without installing
_10
npx create-vite@latest my-app

Clean up after using sudo

If you already used sudo, your npm cache may be owned by root. Give it back to your user:


_10
sudo chown -R "$(id -u):$(id -g)" ~/.npm

Common pitfalls

#
  • Using chmod 777 on /usr/local. It makes the error go away by making system folders writable by everything on the machine.
  • Running sudo chown on all of /usr/local. Other tools, such as Homebrew, manage parts of it and expect specific ownership.
  • Installing nvm but not opening a new terminal, so the old system Node is still first on your PATH.
  • Mixing a version manager with a system Node install, then wondering why which npm points to the wrong one.
  • Seeing EACCES inside a project, not a global install. That usually means node_modules or ~/.npm was created by sudo earlier; fix the ownership instead of adding more sudo.
#

Was this resource helpful?