Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if executable exists on system path with node

Question

Is there a simple way to tell if a system executable is available on the system path using node? For example if a user has python installed at /usr/bin/python and /usr/bin is in $PATH how can I detect that in Node? And conversely detect when something isn't installed or is just not on path, i.e. /usr/opt/local/mycustompath/python? Ideally hoping their is an npm package available ...

I'm sure this is a quick google search with the right search term, but I'm failing due to the fact where and which are pretty generic search terms.

Background

I'm working on some dev config for a node tool and would like to be able to detect whether python (or pip) is already available on path, and if not, ask the user to tell install it or tell us where to find it. I'm currently planning on doing this with where on windows machines and which on *nix machines, but was hoping there might be a single cross platform way of doing this.

like image 943
Ralph Callaway Avatar asked Nov 10 '22 01:11

Ralph Callaway


1 Answers

Package hasbin has since been published to the npm registry, which provides this functionality:

Install it (as part of your project) with npm install hasbin

To test the availability of Python, use it as follows (do not append .exe to the executable file name):

var isPyAvailable = require('hasbin').sync('python')

The package has various other helpful methods, such as the ability to find the first available binary among several - see its GitHub repository.

like image 100
mklement0 Avatar answered Nov 14 '22 21:11

mklement0