Skip to content

Instantly share code, notes, and snippets.

@mscdex
Created January 18, 2014 16:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mscdex/8493112 to your computer and use it in GitHub Desktop.
Save mscdex/8493112 to your computer and use it in GitHub Desktop.
Use npm API from system copy of npm
function loadNpm(cb) {
require('child_process').exec('npm', function(err, stdout, stderr) {
if (err) return cb(err);
var m = /npm@[^ ]+ (.+)\n/i.exec(stdout);
if (!m)
return cb(new Error('Unable to find path in npm help message'));
cb(undefined, require(m[1]));
});
}
// usage ...
loadNpm(function(err, npm) {
if (err) throw err;
// load() is required before using npm API
npm.load(function(err, npm) {
if (err) throw err;
// e.g. npm.search('ssh', true, function(err, results) { console.dir(results); });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment