Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

Better Stack Team
Updated on April 4, 2024

The error Error: EACCES: permission denied, access '/usr/local/lib/node_modules' typically occurs when you try to install global npm packages without the necessary permissions. This can happen if you are trying to install packages using npm install -g without superuser privileges.

Here are a few solutions to fix this issue:

You can use the sudo command to install npm packages with elevated privileges:

 
sudo npm install -g <package-name>

However, using sudo with npm is generally not recommended because it can lead to permission issues and might cause problems later. If you choose this approach, be cautious and understand the implications.

Change npm's default directory

You can configure npm to install global packages in a directory where you have write permissions without using sudo. Choose a directory that does not require superuser privileges.

  1. Create a directory for global npm packages in your home directory:

     
    mkdir ~/.npm-global
    
  2. Configure npm to use this directory:

     
    npm config set prefix '~/.npm-global'
    
  3. Add the following line to your ~/.bashrc or ~/.zshrc file to include the new directory in your PATH:

     
    export PATH=~/.npm-global/bin:$PATH
    
  4. Restart your terminal or run:

     
    source ~/.bashrc
    

Now you should be able to install global npm packages without using sudo:

 
npm install -g <package-name>

Use a Node version manager

Consider using a Node version manager like NVM (Node Version Manager) or Nodenv. These tools allow you to install Node.js and npm without requiring superuser privileges.

Choose the option that best fits your needs and system configuration. Using a version manager is generally recommended for managing Node.js installations and avoiding permission issues.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github