Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node / npm: How to manage globally installed devDependencies

I'm building a Node module with devDependencies that should be globally installed, such as jasmine-node and jshint. What I essentially need is to be able to reference their binaries in my makefile / npm scripts section to run tests, lint, etc. In other words I do not wish to require() them programmatically.

After digging around I'm still confused on how to handle this:

1) My first approach was to assume that these modules would be globally installed, clarify this in my module's documentation and reference their binaries as globals - i.e. expect them to be globally available. This conflicts with this piece of advice

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

(taken from this post)

This generally sounds right, as the aforementioned setup is rather fragile.

2) My next approach was explicitly including those modules in devDependencies (although they are still globally installed on my system (and most probably on users' & contributors' systems as well)). This ensures that appropriate versions of the binaries are there when needed and I can now reference them through node_modules/.bin/.

However I'm now in conflict with this piece of advice

Install it locally if you're going to require() it.

(taken from npm docs)

Regardless of that, I do notice that npm install will now actually fetch nothing (display no network activity) for the globally installed modules.


My questions:

  • Are the local versions of globally installed modules (that are mentioned in devDependencies) just snapshots (copies) of the global ones, taken during npm install?
  • Is 2) the correct way to go about doing this? or is there some other practice I'm missing?
like image 574
biril Avatar asked Jun 20 '13 17:06

biril


People also ask

Does npm install installs devDependencies?

NPM installs devDependencies within the package. json file. The 'npm install' command should add all the dependencies and devDependencies automatically during installation. If you need to add specific devDependencies to your project, you can use this command- 'npm install --save-dev'.

How do I use npm globally?

To install a module from npm globally, you'll simply need to use the --global flag when running the install command to have the module install globally, rather than locally (to the current directory). Note: One caveat with global modules is that, by default, npm will install them to a system directory, not a local one.

How install npm dependencies globally?

Install the dependencies in the local node_modules folder. In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package. By default, npm install will install all modules listed as dependencies in package. json .


1 Answers

Here's my personal take on this, which is decidedly divergent from node.js common practice, but I believe it is an overall superior approach. It is detailed in my own blog post (disclaimer about self-promotion, yada yada) Managing Per-Project Interpreters and the PATH.

It basically boils down to:

  1. Never use npm -g. Never install global modules.
  2. Instead, adjust your PATH to include projectDir/node_modules/.bin instead
like image 135
Peter Lyons Avatar answered Sep 19 '22 21:09

Peter Lyons