简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM