Skip to content

Instantly share code, notes, and snippets.

@cgbystrom
Created October 15, 2014 21:37
Show Gist options
  • Save cgbystrom/3c6c50fdf4072e32817b to your computer and use it in GitHub Desktop.
Save cgbystrom/3c6c50fdf4072e32817b to your computer and use it in GitHub Desktop.
Get a list of all NPM linked packages
var util = require('util');
var exec = require('child_process').exec;
function getNpmLinkedPackages (callback) {
exec('npm list --global', function (error, stdout, stderr) {
if (error) return callback(error);
if (stderr.length > 0) return callback(stderr);
var pkgs = stdout
.split('\n')
.filter(function (line) { return line.split(' ')[0].length == 3; })
.map(function (line) { var l = line.split(' '); return [l[1], l[3]]; })
.reduce(function(pkgs, pkg, i) {
pkgs[pkg[0]] = pkg[1];
return pkgs;
}, {});
callback(undefined, pkgs);
});
}
// Test
getNpmLinkedPackages(function (err, pkgs) {
if (err) return console.error(err);
console.log("Linked packages", pkgs);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment