Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
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.
const which = require('which')
// async usage
// rejects if not found
const resolved = await which('node')
// if nothrow option is used, returns null if not found
const resolvedOrNull = await which('node', { nothrow: true })
// sync usage
// throws if not found
const resolved = which.sync('node')
// if nothrow option is used, returns null if not found
const resolvedOrNull = which.sync('node', { nothrow: true })
// Pass options to override the PATH and PATHEXT environment vars.
await which('node', { path: someOtherPath, pathExt: somePathExt })
Just like the BSD which(1)
binary but using node-which
.
usage: node-which [-as] program ...
You can learn more about why the binary is node-which
and not which
here
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.5.0.0 (2024-10-01)
which
now supports node ^18.17.0 || >=20.5.0
5d49ed0
#151 run template-oss-apply (@reggi)8a2d8e0
#149 bump @npmcli/eslint-config from 4.0.5 to 5.0.0 (@dependabot[bot])d4009b2
#138 bump @npmcli/template-oss to 4.22.0 (@lukekarrys)1a07cd7
#150 postinstall for dependabot template-oss PR (@hashtagchris)45f3aa8
#150 bump @npmcli/template-oss from 4.23.1 to 4.23.3 (@dependabot[bot])FAQs
Like which(1) unix command. Find the first instance of an executable in the PATH.
The npm package which receives a total of 98,250,221 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
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.