简体   繁体   English

使用Node.js构建脚本中的npm进行编程

[英]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. 我有一个大型项目,其中包含多个节点应用程序端点,每个端点都有自己的package.json文件。

I have a main build script (written in jake) which sets up a given environment, runs tests, packages apps etc. 我有一个主要的构建脚本(用jake编写),该脚本设置了给定的环境,运行测试,打包应用程序等。

So is there a way for the root build script to run "npm install" on the given directories. 因此,有一种方法可以让根构建脚本在给定目录上运行“ npm install”。

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 : 是的,看看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. 还可以看一下这个示例 ,它提供了有关如何以编程方式使用npm更多见解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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