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)_10curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash_10_10# Open a new terminal, then_10nvm install --lts_10npm install -g typescript
Why it happens
#The full error looks something like this:
_10npm error code EACCES_10npm error syscall mkdir_10npm error path /usr/local/lib/node_modules/typescript_10npm 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
sudofor 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:
_10fnm install --lts_10fnm use lts-latest_10npm install -g typescript
After switching, check that npm now points at your home folder:
_10which node_10npm 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.
_10mkdir -p ~/.npm-global_10npm config set prefix ~/.npm-global_10_10# Add this line to ~/.zshrc or ~/.bashrc_10export 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.
_10npm install --save-dev typescript_10npx tsc --version_10_10# Run once without installing_10npx 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:
_10sudo chown -R "$(id -u):$(id -g)" ~/.npm
Common pitfalls
#- Using
chmod 777on/usr/local. It makes the error go away by making system folders writable by everything on the machine. - Running
sudo chownon 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 npmpoints to the wrong one. - Seeing EACCES inside a project, not a global install. That usually means
node_modulesor~/.npmwas created bysudoearlier; fix the ownership instead of adding moresudo.
Related reading
#Was this resource helpful?