我有一个节点应用程序,我想在其中运行一个命令或任务,比如npm、安装、rake或git克隆。我尝试使用子进程exec,但没有运行任务。有别的办法吗?
发布于 2015-08-04 12:51:33
如果您想执行一个shell (如果您在cmd上)命令,可以使用child_process.exec()来执行它
下面是一个示例:
var exec = require('child_process').exec;
var child;
child = exec("pwd", function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
        console.log('exec error: ' + error);
    }
});在调用npm install或git clone函数时,只需放置pwd或任何您想要执行的内容,而不是pwd。
发布于 2015-08-04 18:47:00
您可以执行以下操作以使npm install在节点脚本中工作,
执行npm install npm --save (这需要一些时间)
现在,由于npm位于node_modules文件夹中,所以可以在脚本中导入它。下面安装'foobar‘包的示例脚本
var npm = require("npm");
npm.load(function (err) {
      npm.commands.install(["foobar"], function (err, data) {
  });
  npm.on("log", function (message) {
    // progress of the npm install
    console.log(message);
  });
});这只是另一个选择。按照卢西恩的建议使用child_process
https://stackoverflow.com/questions/31809571
复制相似问题