Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Like which(1) unix command. Find the first instance of an executable in the PATH.
The 'which' npm package is a simple utility to locate a command in the user's path, similar to the Unix 'which' command. It is used to find the first instance of a specified executable in the system path, which can be useful for ensuring that a required command-line tool is available and for invoking system commands from Node.js scripts.
Synchronous command search
This feature allows you to synchronously find the path to an executable in the system's PATH. The 'sync' method returns the path to the specified executable if it exists.
const which = require('which');
const cmdPath = which.sync('node');
console.log(`Node.js is located at: ${cmdPath}`);
Asynchronous command search
This feature allows you to asynchronously find the path to an executable in the system's PATH. The 'which' function takes a callback that receives the path to the specified executable if it exists.
const which = require('which');
which('node', function (err, cmdPath) {
if (err) throw err;
console.log(`Node.js is located at: ${cmdPath}`);
});
Throw on not found
This feature allows you to specify whether the 'which' method should throw an error if the command is not found. By default, or when 'nothrow' is set to false, it will throw an error.
const which = require('which');
try {
const cmdPath = which.sync('unknown-command', {nothrow: false});
} catch (e) {
console.error('Command not found');
}
The 'find-exec' package is similar to 'which' in that it helps you find the path to executables in your PATH. However, it has a different API and may offer additional options for searching.
The 'lookpath' package is another alternative to 'which' that provides a way to check for the existence of an executable in the system's PATH. It is designed to be a simple and lightweight solution with a promise-based API.
The 'command-exists' package is used to check if a given command-line tool is available on the system's PATH. Unlike 'which', it does not return the path to the executable but simply a boolean indicating whether the command exists.
Like the unix which
utility.
Finds the first instance of a specified executable in the PATH
environment variable. Does not cache the results, so hash -r
is not
needed when the PATH changes.
var which = require('which')
// async usage
which('node', function (er, resolvedPath) {
// er is returned if no "node" is found on the PATH
// if it is found, then the absolute path to the exec is returned
})
// sync usage
// throws if not found
var resolved = which.sync('node')
// if nothrow option is used, returns null if not found
resolved = which.sync('node', {nothrow: true})
// Pass options to override the PATH and PATHEXT environment vars.
which('node', { path: someOtherPath }, function (er, resolved) {
if (er)
throw er
console.log('found at %j', resolved)
})
Same as the BSD which(1)
binary.
usage: which [-as] program ...
You may pass an options object as the second argument.
path
: Use instead of the PATH
environment variable.pathExt
: Use instead of the PATHEXT
environment variable.all
: Return all matches, instead of just the first one. Note that
this means the function returns an array of strings instead of a
single string.1.3.1
FAQs
Like which(1) unix command. Find the first instance of an executable in the PATH.
The npm package which receives a total of 67,580,878 weekly downloads. As such, which popularity was classified as popular.
We found that which demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 6 open source maintainers 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.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.