如何杀死node.exesubprocess?

我有以下一段代码,用“npm start”参数来启动节点服务器实例:

const childProcess = require("child_process"); // running server before tests before(function(done) { childProcess.exec(["npm start"], function(err, out, code) { if (err instanceof Error) throw err; process.stderr.write(err); process.stdout.write(out); process.exit(); }); setTimeout(done, 5000); }); //run tests require("./customer-individual.js"); require("./customer-organization.js"); 

在testing运行之后,节点服务器实例仍然作为后台进程运行。 我怎么能杀死它?

您可以使用以下内容:

 const child = childProcess.exec(["npm start"], function(err, out, code) { // ... }); child.kill(); // same as child.kill('SIGTERM'); console.log(child.killed); // will log true 

或者任何其他信号,请参阅文档: https : //nodejs.org/api/child_process.html#child_process_child_kill_signal