Use different versions of engine-based NPM dependencies

I need to specify a different version of the dependency based on the Node engine. Something like that:

{
  "node": {
    "0.10.x": {
      "zombie": "2.5.1"
    },
    "0.12.x": {
      "zombie": "^3.5.0"
    }
  },
  "iojs": {
    "^3.0.0": {
      "zombie": "^4.0.0"
    }
  }
}

Is it either built-in, or is there a module that allows you to do this?

+4
source share
1 answer

"Yes, but ..."

Not built in, but possible.

Wise to do? ...

:)

// simplifying for the answer, only looking at node versions...

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){
            // parse the results from ls here, and install or not, etc...
            console.log(lite);
        });
    })
}

, ... , . ( , , , , .)

:

  • semver - ""
  • node - ""
  • npm ""

Vs:

+2

All Articles