1.7. Fixing npm permissions

If you are working on OSX or Linux and your permissions are not set properly you will get an EACCES error when you try to install npm packages globally. You can overcome this issue by using the sudo command to execute the command with administrative privileges but there are other ways to overcome this issue.

How to do it…

  1. The first way to fix this issue is by changing the permission of the npm’s default directory. To do so, we must first find out the npm’s default directory:
    $ npm config get prefix
    
  2. For the majority of systems, the preceding command should return /usr/local as the npm’s default directory. If the npm’s default directory is /usr, you should avoid changing the permission of the folder and switch to the second option.
  3. Once you know the path of the npm’s default directory you need to find out the name of your user account:
    $ whoami
    
  4. You also need to set your user account as the owner of the npm’s default directory. This can be shown in OSX and Linux by executing the following command:
    $ sudo chown –R your_user_name npm_default_directory
    

There’s more…

An alternative way to overcome an EACCES error when you try to install npm packages globally is to changing the npm’s default directory to another directory.

  1. The first thing that you need to do is create a new directory on your user directory (~), which will be used to store the global packages:
    $ mkdir ~/npm-global
    
  2. You also need to change the npm’s default directory in the npm’s configuration:
    $ npm config set prefix '~/npm-global'
    
  3. Open or create a file named .profile located on your user directory (~) and add the following line:
    export PATH=~/npm-global/bin:$PATH
    
  4. Finally, from the command line interface update your system variables:
    source ~/.profile
    

Shiv Kushwaha

Author/Programmer