Search code examples
node.jsnpmjake

Progmatically using npm from nodejs build script


I have a large project which contains multiple node application endpoints, each with their own package.json file.

I have a main build script (written in jake) which sets up a given environment, runs tests, packages apps etc.

So is there a way for the root build script to run "npm install" on the given directories.

I expect psudo code would be:

var npm = require("npm");
var package1Directory = "some-directory";
npm.install(packageDirectory);

Cannot find any documentation around this though so not sure if it is possible... so is it?


Solution

  • Yes, have a look at the docs:

    var npm = require("npm")
    npm.load(myConfigObject, function (er) {
      if (er) return handlError(er)
      npm.commands.install(["some", "args"], function (er, data) {
        if (er) return commandFailed(er)
        // command succeeded, and data might have some info
      })
      npm.on("log", function (message) { .... })
    })
    

    Also have a look at this example, which gives some more insights on how to use npm programmatically.