Created
January 18, 2014 16:55
-
-
Save mscdex/8493112 to your computer and use it in GitHub Desktop.
Use npm API from system copy of npm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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