Node / npm: How to manage globally installed devDependencies

I am creating a Node module with devDependencies, which must be installed globally, for example, jasmine-node and jshint. Essentially, I need to be able to reference my binaries in the makefile / npm script section to run tests, lint, etc. In other words, I do not want () to program them programmatically.

After digging, I was still confused about how to handle this:

1). My first approach was to assume that these modules will be installed globally, check this in my module documentation and specify their binaries as global variables - i.e. expect them to be available worldwide. This contradicts this recommendation.

Make sure you avoid referencing globally installed binaries. Instead, point it to a local node_modules that installs binaries in the hidden .bin directory. Make sure the module (in this case "mocha") is in your package. Json is under devDependencies, so the binary is put there when you run npm install.

(taken from this post )

This usually sounds correct, as the above setting is rather fragile.

2) My next approach was explicitly included in these modules in devDependencies (although they are still globally installed on my system (and, most likely, on user and developer systems). This ensures that suitable binary versions are available when necessary, and now I can reference them through node_modules/.bin/ .

However, I am now in conflict with this advice.

Install it locally if you want () it.

(taken from npm docs )

Regardless, I notice that npm install now actually prints nothing (displays network activity) for global modules.


My questions:

  • Are local versions of global modules (mentioned in devDependencies) only snapshots (copies) of global ones taken during npm install ?
  • Is there 2) the right way to do this? or is there some other practice i'm missing?
+6
source share
2 answers

Repeating my own question a couple of years after it was originally written, I feel that I can now safely say that the β€œadvice” cited

Install it locally if you want () it.

no longer worth it. (This was part of the npm docs, but the published 2-year link gives me 404 at the time of this writing.)

Currently, npm run is a great way to perform task management / automation, and it automatically exports modules that are installed locally to the path before starting. Thus, it makes sense to locally install modules that should not be require()d , such as letters and testers. (By the way, this is fully consistent with the answer that Peter Lyons provided a couple of years ago - perhaps it was "clearly different from the generally accepted practice of" w630> .js ", but today it is quite widely accepted :))

Regarding my second question

Are local versions of global modules (mentioned in devDependencies) only snapshots (copies) of global ones taken during npm installation?

I am pretty sure the answer is No. (Perhaps the lack of network activity that I observed then during the installation of local modules, which were also installed at the global level, was caused by caching ..?)


Note, November 12, 2016

The relevant npm docs that the original question linked to have been moved here .

+1
source

Here is my personal approach to this, which is clearly different from the generally accepted practice of node.js, but I believe that this is a general excellent approach. This is described in detail in my own blog post (giving up self-promotion, yada yada) Managing interpreters for each project and PATH .

It basically boils down to:

  • Never use npm -g. Never install global modules.
  • Instead, configure PATH instead of projectDir/node_modules/.bin
+7
source

Source: https://habr.com/ru/post/947763/


All Articles