EACCES: permission denied – Fixed

The “EACCES” is one of the errors typically encountered when you are trying to install a global npm package like ‘npm install lodash’ in a system or trying to access a file or folder that does not have the necessary permissions to access it. Although there can be many reasons for the occurrence of the error “EACCES: permission denied“, let’s understand why this error is encountered and how to fix it so that your application runs perfectly.

Common causes of “Error: EACCES: permission denied”

Below are the common causes where you might encounter this error-

  1. Insufficient file or directory permissions.
  2. Running your application as the wrong user.
  3. Mistyped or inaccurate file paths.
  4. Incorrect Ownership of file or directory.
  5. Additional security restrictions.

How to resolve the error “EACCES: permission denied”?

Following are the two solutions to resolve the EACCES error when you are trying to install a package globally.

  1. Firstly you can do is Reinstall npm with the node version manager
    OR
  2. Another thing you can do is manually change npm’s default directory.

Steps to Reinstall npm with a node version manager

  1. Install NVM (Node Version Manager).
  2. Install the desired version of Node.js:
    nvm install node
  3. Reinstall npm:
    npm install -g npm
  4. Verify the installations:
    node -v
    npm -v

Steps to Manually change npm’s default directory

  1. Firstly take a Back up of your computer.
  2. Then create a directory for global installations by using the given command.
    mkdir ~/.npm-global
  3. Set npm to use the new directory path by using the given command.
    npm config set prefix ‘~/.npm-global’
  4. Open or create the ~/.profile file by using the given command.
    export PATH=~/.npm-global/bin:$PATH
  5. Update system variables by using the given command.
    source ~/.profile
  6. Test the configuration by using the given command.
    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