如何在 node.js 中执行命令行?

how to execute a command line in node.js?

我使用的是actionhero框架,可以通过命令actionhero start启动。但问题是,我的云node.js应用程序运行ner只能通过指定一个主文件来运行一个应用程序,例如index.js。如何调整我的应用程序以由普通 node.js 文件启动?

你有两个选择

选项#1 您可以使用 child_process 从您的 node.js 应用程序执行任何 shell。请参阅此 How can I invoke Ruby from Node.js? 作为参考。此方法是原生 node.js 方式。您不需要安装任何外部 npm。

选项#2 使用 shelljs 来执行这样的命令。 (https://github.com/arturadib/shelljs)它允许您执行命令的方式与使用 child_process 非常相似,但它使您的代码更整洁。

您正在查看类似...

'use strict';
let child_process = require('child_process');
child_process.execFile('node', [`.\path\to\actionhero`, `start`], (err) => {
  if(err) console.log(err);
});