简体   繁体   中英

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

I would like to implement an external dependency validation logic in my Node JS console application. For example, git. In the terminal "git --version" would respond with current version of the git installed. I might use child_process module in Node and invoke shell commands but is there a better way to do it? It should work regardless of the host operating system.

Why Am I having such requirement?

My application should create a git like merge conflict if 2 or 3 versions (Modified, Original, Remote) of the file having conflicting changes. I wish I could use some node modules to achieve. But it turns that there is none. So I decided to use 'git merge-file'. But before executing the command, I would want to check if git is installed or not. This might seem odd but your suggestions are valuable. Thanks in advance.

Child process is the solution you should go for, as you have already figured it out. It's the only way to interact with other processes from Node.js application.

You can have something like:

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
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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