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…
- 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
- 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.
- Once you know the path of the npm’s default directory you need to find out the name of your user account:
- 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
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.
- 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
- You also need to change the npm’s default directory in the npm’s configuration:
$ npm config set prefix '~/npm-global'
- Open or create a file named
.profilelocated on your user directory (~) and add the following line:
- Finally, from the command line interface update your system variables: