What is command-exists?
The command-exists npm package is used to check if a given command is available in the system's PATH. It is useful for Node.js applications that need to ensure certain commands are installed before attempting to execute them.
What are command-exists's main functionalities?
Synchronous check
This feature allows you to synchronously check if a command exists in the system's PATH.
const commandExistsSync = require('command-exists').sync;
if (commandExistsSync('git')) {
console.log('Git exists!');
} else {
console.log('Git does not exist.');
}
Asynchronous check
This feature allows you to asynchronously check if a command exists in the system's PATH.
const commandExists = require('command-exists');
commandExists('git', function(err, commandExists) {
if(commandExists) {
console.log('Git exists!');
} else {
console.log('Git does not exist.');
}
});
Promise-based check
This feature provides a promise-based interface to check if a command exists, which can be used with modern async/await syntax.
const commandExists = require('command-exists');
commandExists('git')
.then(function(command) {
console.log('Git exists!');
}).catch(function() {
console.log('Git does not exist.');
});
Other packages similar to command-exists
which
The 'which' package is similar to 'command-exists' as it checks for the existence of a command. It is a Node.js implementation of the Unix 'which' command. It differs in that it provides the path to the executable if it exists, rather than just a boolean indicating existence.
lookpath
The 'lookpath' package is another alternative that checks if a command exists in the system's PATH. It is similar to 'command-exists' but returns the full path of the command if it exists. It also supports Windows and is designed to be a lightweight solution.
shelljs
While 'shelljs' is a more comprehensive shell scripting utility library, it includes a 'which' method that can be used to check for the existence of commands, similar to 'command-exists'. It provides additional shell-related functionalities beyond command checking.
command-exists
node module to check if a command-line command exists
installation
npm install command-exists
usage
async
var commandExists = require('command-exists');
commandExists('ls', function(err, commandExists) {
if(commandExists) {
}
});
promise
var commandExists = require('command-exists');
commandExists('ls')
.then(function(command){
}).catch(function(){
});
sync
var commandExistsSync = require('command-exists').sync;
if (commandExistsSync('ls')) {
} else {
}
changelog
v1.2.7
Removes unnecessary printed output on windows.
v1.2.6
Small bugfixes.
v1.2.5
Fix windows bug introduced in 1.2.4.
v1.2.4
Fix potential security issue.
v1.2.0
Add support for promises
v1.1.0
Add synchronous version
v1.0.2
Support for windows