"Yes, but ..."
Not built in, but possible.
Wise to do? ...
:)
var npm = require("npm");
var semver = require("semver");
if (semver.satisfies(process.version, "0.12.x")){
npm.load(null, function(){
installPkg("chalk", "0.5.1");
})
}
function installPkg(pkg, ver) {
if(require.resolve(pkg)){
throw Error("package already installed");
}
var semverPkg = pkg + "@" + ver;
npm.commands.install([semverPkg], function (err, result) {
if (err) console.log("error loading chalk");
});
}
Using NPM is a little frustrating software because it is poorly documented. Semver is really cool when someone else has done all the work for you, but building comparison / satisfaction checks is a tedious job.
, , , , , , , , .
, . , .
-, require.resolve() , , . ( .)
-, npm.commands.ls, :
if (semver.satisfies(process.version, "0.12.x")){
npm.load(null, function(){
npm.commands.ls([], function(err, data,lite){
console.log(lite);
});
})
}
, ... , . ( , , , , .)
:
- semver - ""
- node - ""
- npm ""
Vs: