Node see if Program is installed

2019-05-02 01:16发布

问题:

Hello im making a node app that requires: ffmpeg, node-acoutstid with fpcalc and eye3D. Now is my question how i can see if those 'programs' are installed on the clients machine.

whats the best way to check this ?

回答1:

"shell out" for native detection

In macOS / Linux / bash we'd typically use type -p or command -v (or which if you're doing it wrong).

In windows there's where that you can use like where node.exe.

require('child_process').exec('type -p mything', function (err, stdout) {
  console.log(stdout);
});

This naive approach can work if you're not trying to be cross-platform compatible and if you don't have to worry about sanitizing user input.

Use command-exists

On npm there's a package command-exists. I peeked at the code and it looks like it's probably the simplest cross-platform detection that covers edge cases at a small size:

https://github.com/mathisonian/command-exists/blob/master/lib/command-exists.js

var commandExists = require('command-exists');

// invoked without a callback, it returns a promise
commandExists('ls').then(function (command) {
  // proceed
}).catch(function () {
  // command doesn't exist
});