Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
command-exists
Advanced tools
check whether a command line command exists in the current environment
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.
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.');
});
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.
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.
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.
node module to check if a command-line command exists
npm install command-exists
var commandExists = require('command-exists');
commandExists('ls', function(err, commandExists) {
if(commandExists) {
// proceed confidently knowing this command is available
}
});
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
});
var commandExistsSync = require('command-exists').sync;
// returns true/false; doesn't throw
if (commandExistsSync('ls')) {
// proceed
} else {
// ...
}
Removes unnecessary printed output on windows.
Small bugfixes.
Fix windows bug introduced in 1.2.4.
Fix potential security issue.
Add support for promises
Add synchronous version
Support for windows
FAQs
check whether a command line command exists in the current environment
We found that command-exists demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.