繁体   English   中英

Node JS:如何检查目标机器上是否安装了依赖应用程序?

[英]Node JS: How to check if a dependent application is installed on target machine?

我想在我的 Node JS 控制台应用程序中实现外部依赖验证逻辑。 例如,git。在终端中,“git --version”将响应安装的 git 的当前版本。 我可能会在 Node 中使用 child_process 模块并调用 shell 命令,但有更好的方法吗? 无论主机操作系统如何,它都应该工作。

为什么我有这样的要求?

如果文件的 2 或 3 个版本(修改版、原始版、远程版)发生冲突更改,我的应用程序应创建类似合并冲突的 git。 我希望我可以使用一些节点模块来实现。 但事实证明没有。 所以我决定使用“git merge-file”。 但是在执行命令之前,我想检查是否安装了 git。 这可能看起来很奇怪,但您的建议很有价值。 提前致谢。

子进程是您应该 go 的解决方案,因为您已经知道了。 这是与 Node.js 应用程序的其他进程交互的唯一方法。

你可以有这样的东西:

const { exec } = require('child_process');
exec('git --version', error => {
  if (error) {
     // Git doesn't exist
     // Add your handler code here
  }
  else {
     // Git exists
     // Add remaining of code here
  }
});

暂无
暂无

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

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