Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
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:
mkdir ~/.npm-global
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:
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.
-
Solutions to Common Node.js Errors
This article explores 16 of the most common Node.js errors and discusses possible solutions to each one
Guides -
How to reduce size of node_modules folder?
Reducing the size of the node_modules folder in a Node.js project can be important, especially when deploying applications or managing version control. Here are several strategies you can use to mi...
Questions
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 usBuild 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.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github