How to fix Error: EACCES: permission denied, access ‘/usr/local/lib/node_modules’

Node.js developers frequently encounter the frustrating “EACCES: permission denied” error when using NPM to install packages globally.

This error typically appears when attempting to install packages in protected directories like /usr/local/lib/node_modules, where your user account lacks sufficient permissions.

This comprehensive guide explores multiple solutions to this common issue, explaining not only how to resolve it but why each approach works and which method might be most appropriate for your environment.

Understanding the Error

When you see an error like this:

npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

It indicates that NPM is attempting to write to a directory where your current user doesn’t have write permissions. This typically happens when:

  1. You’re trying to install packages globally (npm install -g)
  2. The default global installation directory requires elevated privileges
  3. Your user account doesn’t have the necessary permissions for that directory

The image below summarizes the solutions covered in this article :

EACCES: permission denied

Read: How to install Node.js on Linux/Ubuntu

Solution 1: Change Ownership of NPM Directories (Not Recommended)

Some developers change ownership of the NPM directories to their current user:

sudo chown -R $(whoami) /usr/local/lib/node_modules

Why this works: This gives your user account complete ownership of the global package directory.

Why it’s not recommended: This approach can create security vulnerabilities and cause permission issues with other applications. It’s a quick fix but introduces potential system stability problems.

Solution 2: Using Sudo (Not Recommended for Regular Use)

The quickest but least recommended solution is to use sudo:

sudo npm install -g <package_name>

Why this works: It runs the npm command with root privileges, bypassing permission restrictions.

Why it’s problematic: Running npm with sudo can lead to:

  • Security vulnerabilities (packages running with root privileges)
  • File permission issues when running non-sudo commands later
  • Difficulties when managing node version changes

Solution 3: Change NPM’s Default Directory (Recommended)

A better approach is to configure NPM to use a directory within your home folder:

# Create a directory for global installations
mkdir ~/.npm-global

# Configure npm to use the new directory
npm config set prefix '~/.npm-global'

# Add the new directory to your PATH
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc

# Update your current session
source ~/.bashrc

Why this works: This solution sets up a user-owned directory for global packages, avoiding permission issues altogether while maintaining security best practices.

Read: Understanding Linux File Permissions: The Complete Guide to Securing Your System Files

Solution 4: Using Node Version Manager (NVM) (Highly Recommended)

Using NVM is perhaps the most elegant solution:

# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

# Install and use a specific Node version
nvm install 16  # Replace with your preferred version
nvm use 16

# Now you can install packages globally without permission issues
npm install -g <package_name>

Why this works: NVM installs Node.js and NPM in your user directory, completely avoiding system directory permission issues. It also provides the added benefit of easily switching between Node.js versions.

Solution 5: Using the --prefix Flag for One-time Installations

For one-off global installations, you can specify a user-writable location:

npm install -g <package_name> --prefix ~/.node_modules

Why this works: This temporarily overrides the default global installation directory.

Why it’s limited: You’ll need to add the bin directory to your PATH, and you’ll have to remember to use the flag each time.

Solution 6: Reinstalling Node.js via Package Manager

For macOS users with Homebrew:

# Uninstall and reinstall Node
brew uninstall node
brew install node

# If you absolutely must use sudo (not recommended)
sudo npm install -g <package_name> --unsafe-perm=true --allow-root

Why this works: Homebrew generally installs packages with appropriate permissions for the current user.

Read: Top 22 Essential Linux Performance Monitoring Tools for System Administrators

Solution 7: Fix Directory Permissions

Identify which specific directories need permission adjustments:

# Check which directories are causing issues
ls -la /usr/local/lib/node_modules
ls -la /usr/local/bin

# Fix permissions for specific directories
sudo chmod -R 775 /usr/local/lib/node_modules
sudo chmod -R 775 /usr/local/bin

Why this works: This grants write access to these directories without changing ownership, which is slightly better than changing ownership but still not ideal.

Best Practices for NPM Global Installations

Based on the solutions above, here are recommended practices:

  1. Avoid using sudo with npm whenever possible
  2. Use NVM for Node.js management – it solves permission issues and provides version flexibility
  3. Configure npm to use a user-directory if you prefer not to use NVM
  4. Use local installations instead of global when possible (using npx for execution)

Verifying Your Solution

After implementing any of these solutions, verify that it works:

# Try installing a simple global package
npm install -g cowsay

# Test if the package works
cowsay "No more permission errors!"

If this works without permission errors, you’ve successfully resolved the issue.

Troubleshooting Persistent Issues

If you’re still experiencing permission errors:

  1. Check environment variables: Ensure your PATH includes the correct directories
  2. Verify npm configuration: Run npm config list to see current settings
  3. Check for conflicting global installations: Sometimes multiple Node.js installations can conflict
  4. Look for file corruption: In rare cases, you might need to clear npm cache with npm cache clean --force

Frequently Asked Questions

Why shouldn’t I use sudo with npm?

Using sudo can create security vulnerabilities because npm packages can contain scripts that would execute with root privileges. Additionally, it often creates inconsistent file permissions that cause problems later.

What’s the difference between local and global npm installations?

Local installations (npm install) place packages in the node_modules directory of your current project, while global installations (npm install -g) place packages in a system-wide directory. Local installations are project-specific, while global installations make packages available system-wide.

How do I know if I need to install a package globally?

Most packages should be installed locally. Global installations are primarily for command-line tools you want to use across projects.

Can I use different versions of npm packages for different projects?

Yes, by installing packages locally (without -g), each project maintains its dependencies, allowing different versions of the same package across projects.

How do I update npm itself?

You can update npm with: npm install -g npm@latest (which might also trigger permission errors that can be solved using the methods in this article).

Do these solutions work for Windows?

Windows has different permission structures. On Windows, you might need to run your terminal as Administrator or modify User Account Control settings. Using NVM for Windows is also an excellent option.

Will changing the global installation directory affect my existing global packages?

You’ll need to reinstall any global packages after changing the npm prefix directory.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Marianne elanotta

Marianne is a graduate in communication technologies and enjoys sharing the latest technological advances across various fields. Her programming skills include Java OO and Javascript, and she prefers working on open-source operating systems. In her free time, she enjoys playing chess and computer games with her two children.

Leave a Reply