Resolving EACCES: permission denied, access ‘/usr/local/lib/node_modules’ Error

In this article we are going to talk about the error EACCES: permission denied, access ‘/usr/local/lib/node_modules’ and also how to troubleshoot it.

Sometimes while working with Node.js you might encounter an error message: Error: EACCES: permission denied, access ‘/usr/local/lib/node_modules’. What does this error mean, and how can you fix it? In this article, we’ll break down this common Node.js permission error into simple words and guide you through the troubleshooting steps.

Also Read: Why does “npm install” rewrite package-lock.json?

Fixing EACCES: permission denied, access ‘/usr/local/lib/node_modules’ Error

First of all, let’s decode the error message. “EACCES” stands for “Error: Access Denied”. It’s a permission issue, indicating that your Node.js application lacks the necessary privileges to access or modify the ‘/usr/local/lib/node_modules’ directory. This directory is where global Node.js packages are typically installed. When Node.js attempts to install a global package, it needs permission to write files and folders in this directory.

Now, let’s explore the troubleshooting steps for resolving this error.

1. Using ‘sudo’ (Linux/macOS)

If you’re on Linux or macOS, you might be tempted to use ‘sudo’ to gain administrative privileges and install global packages. While this can work, it’s not the best practice because it can lead to permission problems later on. However, if you choose this route, here’s what you can do:

sudo npm install -g <package-name>

2. Node Version Manager (NVM)

A more elegant and safe solution is to use Node Version Manager (NVM). NVM allows you to manage different Node.js versions and global packages without needing administrative permissions. Here’s how to do it:

1. Install NVM – Visit the official NVM GitHub repository and follow the installation instructions, which typically involve running a script.

2. Install Node.js – After installing NVM, you can easily install and switch between different Node.js versions with simple commands like:

nvm install <node-version>
nvm use <node-version>

3. Install Global Packages – Once you have Node.js set up via NVM, you can install global packages without needing administrative permissions:

npm install -g <package-name>

3. Changing npm’s Default Directory

If you’d rather not use NVM but still want to avoid ‘sudo,’ you can change npm’s default directory to one where you have write permissions. Follow these steps:

1. Change npm’s prefix – Specify a different directory where you have write access:

npm config set prefix ~/.npm-global

This command configures npm to use the ‘~/.npm-global’ directory for global package installations.

2. Update your PATH – You’ll also need to add the ‘~/.npm-global/bin’ directory to your system’s PATH variable to execute globally installed packages:

export PATH=$PATH:~/.npm-global/bin

After completing these steps, you should be able to install global packages without needing administrative privileges.

4. Fixing File Permissions

Sometimes, permission issues occur due to incorrect file permissions. You can try to fix this by changing the permissions on the ‘/usr/local/lib/node_modules’ directory. To grant yourself access, use the following command:

sudo chown -R $USER /usr/local/lib/node_modules

Replace $USER with your username. This command recursively changes ownership of the ‘/usr/local/lib/node_modules’ directory and its contents to your user account.

Conclusion

Now we have reached to the end of this article. Hope it has elaborated on troubleshooting EACCES: permission denied, access ‘/usr/local/lib/node_modules’. It’s a common issue with straightforward solutions. While using ‘sudo’ can temporarily resolve the problem, it’s not the ideal approach, as it can lead to future permission complications. Node Version Manager (NVM) offers a safer and more convenient method to manage Node.js versions and packages without administrative privileges. Alternatively, you can change npm’s default directory or fix file permissions to address this error without compromising the stability of your system. These solutions empower you to navigate the world of Node.js development with confidence.

Node.js is a powerful tool for web development, and understanding and conquering common issues like permission errors is key to unleashing its full potential. For more such articles on Node.js follow https://codeforgeek.com/.

References

Arnab Biswas
Arnab Biswas
Articles: 10