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:
Use sudo
(not recommended)
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.
- Create a directory for global npm packages in your home directory:
- Configure npm to use this directory:
npm config set prefix '~/.npm-global'
- Add the following line to your
~/.bashrc
or~/.zshrc
file to include the new directory in your PATH:
export PATH=~/.npm-global/bin:$PATH
- Restart your terminal or run:
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.
Got an article suggestion? Let us know
Explore more
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
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.
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working…