1

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.

  • Isn't that the same with, for example, Python or Ruby? While you can install packages globally, you need root access. These days you'd recommend people to either install packages in a per-user directory, or use Node/Python/Ruby version managers to avoid messing with system-level packages. (My personal feeling is that it's still way too easy to tell people to “just use sudo” and have them break their system Python.) – slhck May 24 '19 at 14:42
  • @slhck, it might be the way you wrote; specifically, It may be the case of my understanding of the term "globally". For me, in the context of npm, "globally" means "for all projects" (e.g., that way "in /home" means "globally"). Should I understand it as "for all users"? Does npm understand "globally" that way? – Silv May 24 '19 at 21:06
  • Yes, “globally” means “for all users”, not “for all projects”. – slhck May 25 '19 at 10:23
  • @slhck, could you give a link to some source of this information, or at least indicating that? The npm docs seems not to be very helpful in this case for a beginner like me, however now I noticed that it states: "Installing a package globally allows you to use the code in the package as a set of tools on your local computer" (a fairly general info). In the case you wrote, it'd be naturally sensible to me to have the directory /usr/lib/node_modules protected with root privileges when writing. I would agree it should be like this using any application, not just npm. – Silv May 25 '19 at 15:16
  • PS. @slhck, so, if I understand it correctly: since you wrote that you personally think it is better not to encouraging users to install packages globally, do you know why npm recommend the use of nvm, instead of just changing the behavior of the global install mode? It might change this behavior to, e.g., the same as nvm has, or at least only some part of the nvm behavior. – Silv May 25 '19 at 15:28
1

You didn't say what user you ran that as, but I assuming that it wasn't root? In which case the error is correct, but also expected because normal users shouldn't be able to install to system directories.

In any you should never use npm to install packages globally with the Fedora packaged Node.js if only because it will leave you with a confused mess of rpm managed and npm managed modules in /usr/lib/node_modules.

Unfortunately unlike other language environments Node.js does not support having two separate global module directories (one in /usr for modules managed by the system packaging and one in /usr/local for modules managed by the language environment's own tooling) so we are not able to make this work in a sensible way without extensive patching of npm that we don't wish to do. Even if such a split was possible you would still need to be root to do a global install though, just as you do with perl or python or ruby.

As I understand it global installation is discouraged by Node.js anyway - the view of upstream is that you should installed locally in each project just the modules that the project needs.

  • Thanks for the answer! Yes, I forgot to mention that I run commands as a normal user, not root. I've updated my post. You wrote "because it will leave you with a confused mess of rpm managed and npm managed modules in /usr/lib/node_modules" – I didn't think about it that way. But I thought that node_modules/ contains only node modules, and if so, what could be the danger? And I don't fully understand you next paragraph; what are "modules managed by the system packaging"? I thought that "modules" always mean "node modules", and that packages managed by the system are a different concept. – Silv May 24 '19 at 14:32
  • Well I mean modules installed from rpms - say if you do dnf install nodejs-is-function to install the is-function moules from the rpm packaged version. – TomH May 24 '19 at 17:55
  • I did not think about it that way, thanks. It may be true that could cause a (little) mess. But I am still confused. You wrote that this error is expected. If the real cause for this error is this probable mess (I might understand that), shouldn't Fedora install npm packages into another directory? Speaking differently, is it the case of npm trying to install modules into node_modules/ (while it should not on Fedora), or is it the case of Fedora trying to install rpm packages into this directory (while it should not in case of Node modules)? – Silv May 24 '19 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.