How to Fix npm EACCES Error Without Using Sudo
Stop using sudo for npm global packages. Learn how to fix the npm EACCES error permanently using nvm for secure, user-level Node.js environment management.
If you’ve ever run npm install -g only to be greeted by a wall of red text ending in EACCES, you know the frustration. It usually happens when you try to install a global package and npm hits a permission wall because it’s trying to write to a system-owned directory.
The most common "fix" you'll find on forums is sudo npm install -g. Don’t do it. Using sudo with npm is a security nightmare that changes the ownership of files in your local directory to root, which will inevitably come back to haunt you when you try to run standard commands later.
Why the npm EACCES error happens
The npm EACCES error occurs because your user account doesn't have write access to the directory where npm tries to store global binaries and modules—usually /usr/local/lib/node_modules. This directory is often owned by the root user by default on macOS and Linux.
When you force the issue with sudo, you aren't just installing a package; you're running arbitrary code from the internet with root-level privileges. If that package has a malicious script, it has full access to your system. Beyond security, it leads to messy node permissions issues where your regular user account can no longer update or remove packages without sudo, creating a permanent dependency on elevated privileges.
The right way: Use nvm
The industry-standard solution for managing node permissions is to move your Node installation into your home directory using a version manager like nvm (Node Version Manager). This ensures that all global packages are installed in a folder you own, requiring zero sudo access.
Step 1: Install nvm
If you haven't installed it yet, use the official install script:
Bashcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
After installing, restart your terminal or source your config file (like .bashrc or .zshrc).
Step 2: Install Node.js
Now, install a version of Node.js through nvm:
Bashnvm install --lts nvm use --lts
Because nvm installs Node inside ~/.nvm, your user account owns the entire directory structure. You can now run npm install -g <package> without ever touching sudo.
Alternative: Changing the npm default directory
If you can't use nvm for some reason, you can tell npm to use a different directory for global packages—one that you own.
- Create a directory for your global packages:
Bash
mkdir ~/.npm-global - Configure npm to use this new path:
Bash
npm config set prefix '~/.npm-global' - Update your shell configuration (e.g.,
~/.zshrcor~/.bashrc) to include this directory in yourPATH:Bashexport PATH=~/.npm-global/bin:$PATH - Source your config file:
Bash
source ~/.zshrc
A quick comparison of approaches
If you are still struggling with dependencies, it might be worth checking my guide on Fixing npm Module Not Found Errors: A Practical Debugging Guide to ensure your pathing is correct.
| Method | Security | Maintenance | Recommended |
|---|---|---|---|
| Sudo npm | Poor | Very High | Never |
| nvm | Excellent | Low | Yes |
| Custom Prefix | Good | Moderate | Maybe |
Final thoughts
I used to rely on sudo for years until I encountered a broken system update that left me unable to even run npm list without root access. It took me about two hours to clean up the ownership issues across my system. Since switching to nvm, I haven't seen an npm EACCES error in years.
While nvm is my go-to, remember that managing environments is only half the battle. If you're building complex apps, ensure your deployment pipeline is also secure—sometimes you need professional help with CI/CD Pipeline & Docker Containerization to keep your development and production environments consistent.
FAQ
Q: Does nvm work on Windows?
A: nvm is for Unix-based systems. For Windows, use nvm-windows. It provides a similar experience and keeps your global packages in a user-accessible directory.
Q: Is it safe to change the permissions of /usr/local/lib/node_modules?
A: You can use chown to give your user ownership, but it's risky. It's better to isolate your Node environment entirely rather than messing with system-level folder ownership.
Q: I still get errors after using nvm, why?
A: You might have stale global packages installed with sudo. You may need to manually delete the old /usr/local/lib/node_modules and symlinks in /usr/local/bin to clear them out once and for all.