Home

Awesome

npm-dep-chain

Description

Get the dependency chain for a specified Node module (or a collection of modules). Think of it as a remote npm ls.

API

getDepChain(package[s], options, callback), where:

Use cases

Usage

basic example:

/**
* Get all dependencies for multiple versions of Express and NPM latest
*/
var getDepChain = require('npm-dep-chain'),
    util = require('util');

getDepChain([
  {
    name    : 'express',
    version : '3.1.x'
  }, {
    name    : 'express',
    version : '3.4.x'
  }, {
    name    : 'npm',
    version : 'latest'
  }
], function(err, pkgs) {
  if (err) {
    throw err;
  }

  console.log(util.inspect(pkgs, { depth: 2 }));
});

using a filter function:

var getDepChain = require('npm-dep-chain'),
    util = require('util');

getDepChain({
  name    : 'express',
  version : '3.1.x'
}, function filter(pkg, cb) {
  // exclude some modules
  if (pkg.name === 'debug' || pkg.name === 'fresh' || /^co/.test(pkg.name)) {
    return cb(null, false);
  }

  cb(null, true);
}, function(err, pkgs) {
  if (err) {
    throw err;
  }

  console.log(util.inspect(pkgs));
});

More examples

Look in the /examples folder, there are more complex examples there.

Motivation

I wanted to create my private NPM registry with custom replication, so I needed to know the dependency tree for a module in order to replicate it (along with all its subdependencies) to that private registry.

How does it work?

It goes to the NPM registry to get a module's dependencies (or even for more modules), then repeats the dependencies found and so on. It uses a cache (by default stored in __dirname + '/cache') so that if you get the info for a module it won't go to the registry a second time for the same module (but in case there is no compatible version found for the module based on the cached info, it will refresh the cache). For more info, checkout npm-pkginfo, which is used internally.

Tests

npm test

License

MIT