https://github.com/otiai10/lookpath
The minimum and most straightforward way to check if command exists and where the executable is, without spawning child_process.
https://github.com/otiai10/lookpath
binary command executable exists nodejs path where which
Last synced: 12 months ago
JSON representation
The minimum and most straightforward way to check if command exists and where the executable is, without spawning child_process.
- Host: GitHub
- URL: https://github.com/otiai10/lookpath
- Owner: otiai10
- License: mit
- Created: 2019-04-19T01:22:01.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2025-04-01T07:50:38.000Z (about 1 year ago)
- Last Synced: 2025-04-04T02:07:57.658Z (about 1 year ago)
- Topics: binary, command, executable, exists, nodejs, path, where, which
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/lookpath
- Size: 1.07 MB
- Stars: 63
- Watchers: 2
- Forks: 8
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# lookpath
[](https://badge.fury.io/js/lookpath)
[](https://www.npmjs.com/package/lookpath)
[](https://github.com/otiai10/lookpath/actions/)
[](https://github.com/otiai10/lookpath/actions/workflows/codeql-analysis.yml)
[](https://codecov.io/gh/otiai10/lookpath)
[](https://codeclimate.com/github/otiai10/lookpath/maintainability)
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fotiai10%2Flookpath?ref=badge_shield)
To check if the command exists and where the executable file is, **WITHOUT** using `child_process`.
```
npm install lookpath
```
# Example usage
```js
const { lookpath } = require('lookpath');
const p = await lookpath('bash');
// "/bin/bash", otherwise "undefined"
```
# Advanced usage
```js
const p = await lookpath('bash', {
include: ['/home/hiromu/.bin'],
exclude: ['/mnt']
});
// include: Do scan also under `~/.bin`
// exclude: Do NOT scan under `/mnt`
```
# Motivation
- I don't want to spawn `child_process` in order to kick `which`, `where`, `whereis`, or `command -v`.
- [node.js - Node - check existence of command in path - Stack Overflow](https://stackoverflow.com/questions/34953168/node-check-existence-of-command-in-path/)
- [Node.js: Check if a command exists - Gist](https://gist.github.com/jmptable/7a3aa580efffdef50fa9f0dd3d068d6f)
- [mathisonian/command-exists: node module to check if a command-line command exists - GitHub](https://github.com/mathisonian/command-exists)
- then I checked Go implementation of [`exec.LookPath`](https://golang.org/pkg/os/exec/#LookPath).
- [src/os/exec/lp_unix.go - The Go Programming Language](https://golang.org/src/os/exec/lp_unix.go?s=928:970#L24)
- so I concluded that scanning under `$PATH` or `$Path` is the best straightforward way to check if the command exists.
# Issues
- https://github.com/otiai10/lookpath/issues
Any feedback would be appreciated ;)
# License
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fotiai10%2Flookpath?ref=badge_large)