Monday, July 17, 2017

linux - In npm, why is there a directory with root priviliges set by default for global packages?

I am currently learning Angular and npm. Today I tried to do npm i -g @angular/cli on my Fedora, but instead having it installed, I saw in the terminal (I have shortened the message for convenience):


npm WARN checkPermissions Missing write access to /usr/lib/node_modules

So, I checked in Google what does it mean. I found this npm explanation, and this article led me to another npm article describing this error in more details. The latter article stated that:



If you see an EACCES error when you try to install a package globally, you can either:


Reinstall npm with a node version manager (recommended),


or


Manually change npm’s default directory



As a quick fix, both of the above solutions looked OK.


But as I have some experience with Linux, I started to wonder: why do I have to install another software, or why do I have to even change some configuration? Speaking differently, why does the default configuration for npm and the default configuration for Fedora require me to adjust even one of them in order not to have any errors?


After asking myself those questions, I thought that the case might be in my npm configuration. Most probably I have not changed anything since the installation, but who knows? So, I decided to reinstall npm. To be sure, I rebooted the computer after uninstalling. After reinstalling, I tried npm i -g @angular/cli again – but the same error occured.


Then I thought: maybe it is not the case of npm, maybe it is the case of my Fedora installation? I certainly did not want to reinstall Fedora for such a reason. So, I checked in Google whether Fedora's default permissions for the directory /usr/lib/node_modules are the way they are by default. For my directory, for both /usr, /usr/lib and /usr/lib/node_modules (and even for /usr/lib/node_modules/npm) the owner and group was root root and only the owner has write permissions. The only source which I found was "Fedora 26 Installation Guide" (although I have Fedora 29 installed), and that document was advising me to review the Filesystem Hierarchy Standard 2.3 for the directory structure. Since I expected node_modules to have assigned exactly the permissions that npm desires, I checked only /usr and /usr/lib. For the /usr directory, FHS did not seem to state anything about permissions; for the /usr/lib directory, it stated that:



/usr/lib includes object files, libraries, and internal binaries that are not intended to be executed directly by users or shell scripts. [22]



This information was also not very helpful.


Eventually, I came to the conclusion that I still do not understand what is the real cause for the error I got. So now I would like to ask:



  1. Is it that npm must be configured by default the way that it has to put the new packages in /usr/lib/node_modules?

  2. If yes, is it that Fedora has set some other default permissions that it should/shall/may have for those directories?

  3. If no, is it that I might have had accidentally changed permissions for my Fedora installation for those directories?

  4. If no, is it the case of me not installing npm packages with root permissions? This seems to also be a fix, but the npm documentation does not mention such a solution.

  5. If no, is it the case of me trying to install npm packages globally while I should not? If so, why is there such possibility?

  6. Is there something that I do not understand or I am missing? Maybe I lack of some (up-to-date) documentation?




UPDATE: I forgot to point out that I run all commands as a normal user, not root.

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...