[Ubuntu] npm permissions

Are you using any Debian based linux machine ?

      - If you say Yes,  you might face the permission issue with NPM.

Issue is every time I run npm command it's getting EACCESS error.
It means I have to run the command as a super user mode, but I don't like that.

             sudo npm install [module name]

I have been facing this issue from couple of days.

Finally, I decided to fix this issue.

Let's see how did I fix the issue.
  There are couple of ways to fix this issue:

  1.     Change the permission to npm's default directory.
  2.     Change npm's default directory to another directory.
  3.     Install node with a package manager that takes care of this for you.



I think first one is simple.
As far as I know this command should work.

              sudo chown -R kernellora /usr/local/lib/node_modules/


 I followed the second option which is changing the npm default directory.

The reason  I choose the second option is :

There are times when you do not want to change ownership of the default directory that npm uses (i.e. /usr) as this could cause some problems, for example if you are sharing the system with other users.

Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.

Step-1  Make a directory for global installations:


              mkdir  .npm-global

Step-2  Configure npm to use the new directory path:


              npm config set prefix '.npm-global'

Step-3  Open or create a .profile file and add this line:


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

Step-4  Back on the command line, update your system variables:

               source .profile

Instead of steps 2-4 you can also use the corresponding ENV variable (e.g. if you don't want to modify .profile):
              NPM_CONFIG_PREFIX=.npm-global
 
I don't want to change the profile file so I used above command.

Now my problem is fixed :-).



No comments:

Post a Comment