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

How we solve Error: EACCES: permission denied erros and more solutions from other sources that may work for you.

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

If you run into this type of error, this solution is how i solved it . Just run this command in your term :

sudo npm install -g @ angular / cli

This is how i resolved it when i was installing and configuring angular on my ubuntu.

The problem is that , this solution is not working for everyone.

The best way to resolve EACCES permissions errors when installing packages globally is to install npm with node version manager or manually change npm’s default directory.

Let’s start with the easiest part of the Jobs

How to install NVM ( Node Version Manager )?

If you own an ubuntu or debian based distribution , execute these script . You dont need to change your current node js installation.

To install or update nvm, you should run the install script. To do that, you may either download and run the script manually, or use the following cURL or Wget command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

Running either of the above commands downloads a script and runs it.

The script clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile~/.zshrc~/.profile, or ~/.bashrc).

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

How to use NVM ?

nvm allows you to quickly install and use different versions of node via the command line.

Example:

$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6

Simple as that!

If you don’t have linux or want to learn more about NVM , go to https://github.com/nvm-sh/nvm

Manually change npm’s default directory

These steps doesn’t work on Microsoft Windows.

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use hidden directory in your home directory.

  • Back up your computer.
  • On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
  • Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
  • In your preferred text editor, open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
  • On the command line, update your system variables:
source ~/.profile
  • To test your new configuration, install a package globally without using sudo:
npm install -g jshint
  • Instead of steps 3-5, you can use the corresponding ENV variable (e.g. if you don’t want to modify ~/.profile):
NPM_CONFIG_PREFIX=~/.npm-global

These steps are extract from docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

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

Leave a Comment