Home / Localhost / npm EACCES permission denied

npm EACCES: permission denied when installing packages globally

Fix npm "EACCES: permission denied, mkdir /usr/local/lib/node_modules" without sudo: change the npm folder, use nvm or fix ownership.

This error means npm tried to write to a folder that your user cannot change, usually the system folder for global packages. Running npm with sudo seems to work but it causes more permission problems later. Give npm a folder you own instead.

What to do, step by step

  1. Read the path in the error. If it is /usr/local/lib/node_modules or /usr/lib, npm is trying to install globally into a system folder.
  2. The cleanest fix is a Node version manager such as nvm. It installs Node in your home folder, so no sudo is needed.
  3. Or give npm its own global folder: create it, point npm at it, and add it to your PATH (commands below).
  4. If the path is inside ~/.npm or your project, an earlier sudo run left root-owned files. Take them back with the chown command below.
  5. Run the install again without sudo.

Commands you can copy

Use your own folder for global packages
mkdir -p ~/.npm-global
npm config set prefix ~/.npm-global
Add that folder to PATH (bash or zsh profile)
export PATH=~/.npm-global/bin:$PATH
Give your user back the npm cache
sudo chown -R $(id -u):$(id -g) ~/.npm
Check which folder npm uses
npm config get prefix

Other causes to check

EACCES on node_modules in your project
An earlier sudo npm install made root-owned files. Run sudo chown -R $(id -u):$(id -g) node_modules or delete the folder and install again without sudo.
Docker bind mount, permission denied
The container user and your user have different IDs. Run the container with your user ID, or fix the ownership of the mounted folder.
Windows: EPERM or EACCES
A program such as antivirus or an editor is holding the file. Close it, delete node_modules and try again, or run the terminal as administrator once.
The command is not found after the install
The new global folder is not in your PATH. Add ~/.npm-global/bin to PATH in your shell profile.

Questions about npm EACCES permission denied

Why should I not use sudo with npm?
Packages and scripts would run as root and can create root-owned files in your home folder. That breaks later installs and is a security risk.
What is nvm?
Node Version Manager. It installs several Node versions in your home folder and lets you switch between them, with no sudo.
Where does npm keep its cache?
In ~/.npm on Linux and macOS. If it is owned by root, the chown command here fixes it.

Updated 2026-10-11

Config generators for this

Related pages