❌ The Problem You're Facing
You're trying to install a package globally and you see this error:
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules'
This happens because npm is trying to write to a directory that requires administrator/root permissions.
If you're seeing this error, don't just run sudo npm install. That's a temporary fix that creates bigger problems later. I'll show you 5 proper solutions, ranked from best to worst.
Why This Error Happens
When you install Node.js using the official installer or a package manager, it often places global packages in directories like:
/usr/local/lib/node_modules(macOS/Linux)/usr/lib/node_modules(Linux)C:\Program Files\nodejs\node_modules(Windows)
These directories are owned by root/administrator, so your regular user account can't write to them without elevated permissions.
⚠️ Why NOT to Use sudo npm install
Running npm with sudo can:
- Create files owned by root in your home directory
- Cause permission issues with future npm commands
- Allow malicious packages to install system-wide malware
- Break your Node.js installation
Use one of the solutions below instead.
Solution 1: Use a Node Version Manager (Recommended)
1 Best Solution: Node Version Manager
This is the recommended approach by the Node.js team. It installs Node in your home directory, so you never need sudo.
For macOS: Use nvm (Node Version Manager)
Step 1: Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Step 2: Restart your terminal or run:
source ~/.zshrc # or ~/.bash_profile
Step 3: Install Node.js
nvm install --lts
nvm use --lts
Step 4: Verify it works
npm install -g typescript # No sudo needed!
💡 Pro Tip
Add this to your ~/.zshrc to automatically use the LTS version:
echo 'nvm use --lts' >> ~/.zshrc
For Linux: Use nvm (Node Version Manager)
Step 1: Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Step 2: Restart your terminal or run:
source ~/.bashrc
Step 3: Install Node.js
nvm install --lts
nvm use --lts
Step 4: Verify it works
npm install -g typescript # No sudo needed!
For Windows: Use nvm-windows
Step 1: Download nvm-windows installer
Go to nvm-windows releases and download nvm-setup.exe
Step 2: Run the installer
Follow the installation wizard (accept all defaults)
Step 3: Open a new Command Prompt or PowerShell and install Node:
nvm install lts
nvm use lts
Step 4: Verify it works
npm install -g typescript # No admin needed!
Solution 2: Change npm's Default Directory
2 Alternative: Change npm's Global Directory
If you can't use a version manager, you can configure npm to use a directory you own.
Step 1: Create a directory for global packages
mkdir ~/.npm-global
Step 2: Configure npm to use this directory
npm config set prefix '~/.npm-global'
Step 3: Add to your PATH
For bash (Linux/macOS):
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
For zsh (macOS):
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
Step 4: Test it
npm install -g typescript # Should work without sudo
Solution 3: Use npx Instead of Global Installs
3 Modern Approach: Use npx
For many tools, you don't need to install them globally at all. Use npx to run them directly.
Instead of:
npm install -g create-react-app
create-react-app my-app
Use:
npx create-react-app my-app
Benefits:
- No global installation needed
- Always uses the latest version
- No permission issues
- Keeps your system clean
Common tools that work with npx:
npx create-react-appnpx create-next-appnpx create-vitenpx eslintnpx prettier
Solution 4: Fix Permissions on Existing Installation
4 Quick Fix: Change Directory Ownership
If you already have Node installed and just want to fix permissions quickly.
For macOS/Linux:
# Find npm's global directory
npm config get prefix
# Change ownership to your user (replace /usr/local with your prefix)
sudo chown -R $(whoami) /usr/local/lib/node_modules
sudo chown -R $(whoami) /usr/local/bin
sudo chown -R $(whoami) /usr/local/share
⚠️ Use with Caution
This solution works but is not as clean as using a version manager. Only use this if you understand what you're doing.
Solution 5: Use Homebrew (macOS Only)
5 macOS Alternative: Homebrew
If you're on macOS and use Homebrew, it handles permissions correctly.
Step 1: Uninstall existing Node (if installed via official installer)
# Remove Node.js installed from nodejs.org
sudo rm -rf /usr/local/bin/node
sudo rm -rf /usr/local/lib/node_modules
Step 2: Install Node via Homebrew
brew install node
Step 3: Test it
npm install -g typescript # Should work without sudo
Which Solution Should You Use?
| Solution | Best For | Difficulty |
|---|---|---|
| 1. Node Version Manager | Everyone (recommended) | Easy |
| 2. Change npm Directory | Can't use version manager | Easy |
| 3. Use npx | One-time tool usage | Easiest |
| 4. Fix Permissions | Quick fix for existing setup | Medium |
| 5. Homebrew (macOS) | macOS Homebrew users | Easy |
Preventing Future Permission Issues
Once you've fixed the issue, follow these best practices:
- Never use
sudo npm install- It creates more problems than it solves - Use a version manager - It's the cleanest solution
- Prefer npx for one-time tools - Don't clutter your global packages
- Use
npm ciinstead ofnpm installin CI/CD pipelines - Keep global packages minimal - Install most packages locally in projects
Troubleshooting Common Issues
"command not found" after installing globally
If you installed a package globally but can't run it:
# Check where global packages are installed
npm config get prefix
# Make sure the bin directory is in your PATH
echo $PATH
# Add it if missing (example for ~/.npm-global)
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
Still getting permission errors after using nvm
This usually means you have an old Node installation conflicting:
# Check which node is being used
which node
which npm
# If it's not in ~/.nvm, uninstall the system Node
# macOS: Remove from /usr/local/bin
# Linux: sudo apt remove nodejs (or your package manager)
# Windows: Uninstall from Programs & Features
Summary
The EACCES error happens because npm is trying to write to a protected directory. The best solution is to use a Node version manager (nvm or nvm-windows), which installs Node in your home directory where you have full permissions.
Quick recap:
- ✅ Best solution: Use nvm/nvm-windows
- ✅ Quick alternative: Change npm's global directory
- ✅ Modern approach: Use npx instead of global installs
- ❌ Avoid: Using
sudo npm install
Still having issues? Contact us and we'll help you troubleshoot.