Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
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')
// 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.v1.2.9
is-absolute
moduleFAQs
Like which(1) unix command. Find the first instance of an executable in the PATH.
The npm package which receives a total of 67,196,957 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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.