2015-05-22 03:14:39 -04:00
|
|
|
# which
|
|
|
|
|
|
|
|
Like the unix `which` utility.
|
2011-11-21 09:48:45 -08:00
|
|
|
|
|
|
|
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.
|
2015-05-22 03:14:39 -04:00
|
|
|
|
|
|
|
## USAGE
|
|
|
|
|
|
|
|
```javascript
|
2022-12-06 22:18:33 -05:00
|
|
|
const which = require('which')
|
2015-05-22 03:14:39 -04:00
|
|
|
|
|
|
|
// async usage
|
2022-12-06 22:18:33 -05:00
|
|
|
// rejects if not found
|
|
|
|
const resolved = await which('node')
|
2015-05-22 03:14:39 -04:00
|
|
|
|
2022-12-06 22:18:33 -05:00
|
|
|
// if nothrow option is used, returns null if not found
|
|
|
|
const resolvedOrNull = await which('node', { nothrow: true })
|
2020-10-02 17:52:19 -04:00
|
|
|
|
2015-05-22 03:14:39 -04:00
|
|
|
// sync usage
|
|
|
|
// throws if not found
|
2022-12-06 22:18:33 -05:00
|
|
|
const resolved = which.sync('node')
|
2015-05-22 03:14:39 -04:00
|
|
|
|
2017-10-26 22:35:25 -04:00
|
|
|
// if nothrow option is used, returns null if not found
|
2022-12-06 22:18:33 -05:00
|
|
|
const resolvedOrNull = which.sync('node', { nothrow: true })
|
2017-10-26 22:35:25 -04:00
|
|
|
|
2015-05-22 03:14:39 -04:00
|
|
|
// Pass options to override the PATH and PATHEXT environment vars.
|
2022-12-06 22:18:33 -05:00
|
|
|
await which('node', { path: someOtherPath, pathExt: somePathExt })
|
2015-05-22 03:14:39 -04:00
|
|
|
```
|
|
|
|
|
2016-01-28 18:11:35 -08:00
|
|
|
## CLI USAGE
|
|
|
|
|
2023-05-07 03:37:34 -07:00
|
|
|
Just like the BSD `which(1)` binary but using `node-which`.
|
2016-01-28 18:11:35 -08:00
|
|
|
|
|
|
|
```
|
2023-05-07 03:37:34 -07:00
|
|
|
usage: node-which [-as] program ...
|
2016-01-28 18:11:35 -08:00
|
|
|
```
|
|
|
|
|
2023-05-07 03:37:34 -07:00
|
|
|
You can learn more about why the binary is `node-which` and not `which`
|
|
|
|
[here](https://github.com/npm/node-which/pull/67)
|
|
|
|
|
2015-05-22 03:14:39 -04:00
|
|
|
## OPTIONS
|
|
|
|
|
2015-10-29 16:50:12 -07:00
|
|
|
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.
|