https://github.com/ehmicky/get-bin-path
Get the current package's binary path
https://github.com/ehmicky/get-bin-path
bin binary cli cwd dependency-management es6 executable javascript library nodejs npm package package-json package-manager path shell terminal testing typescript unit-testing
Last synced: 6 months ago
JSON representation
Get the current package's binary path
- Host: GitHub
- URL: https://github.com/ehmicky/get-bin-path
- Owner: ehmicky
- License: apache-2.0
- Created: 2019-07-01T11:02:12.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-09-14T16:52:53.000Z (about 1 year ago)
- Last Synced: 2024-10-11T10:17:07.738Z (about 1 year ago)
- Topics: bin, binary, cli, cwd, dependency-management, es6, executable, javascript, library, nodejs, npm, package, package-json, package-manager, path, shell, terminal, testing, typescript, unit-testing
- Language: JavaScript
- Homepage:
- Size: 10.8 MB
- Stars: 36
- Watchers: 3
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README

[](https://www.npmjs.com/package/get-bin-path)
[](/src/main.d.ts)
[](https://codecov.io/gh/ehmicky/get-bin-path)
[](https://fosstodon.org/@ehmicky)
[](https://medium.com/@ehmicky)
Get the current package's binary path (using the `package.json`
[`bin`](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#bin) or
[`directories.bin`](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#directoriesbin)
field).
This is useful when testing a package's binary. Using `get-bin-path` (as opposed
to hard-coding the path to the binary):
- validates that the `package.json` `bin` or `directories.bin` field is
correctly setup
- decouples the binary path from the tests, which allows moving the file without
rewriting the tests
# Examples
```js
import { getBinPath } from 'get-bin-path'
// `binPath` is the absolute path to the current package's binary
const binPath = await getBinPath()
```
```js
// Test runner
import test from 'ava'
// Library to execute child processes / commands
import { execa } from 'execa'
import { getBinPath } from 'get-bin-path'
const binPath = await getBinPath()
test('Binary file should return "true"', async (t) => {
const { stdout } = await execa(binPath)
t.is(stdout, 'true')
})
```
# Install
```
npm install get-bin-path
```
This package works in Node.js >=18.18.0.
This is an ES module. It must be loaded using
[an `import` or `import()` statement](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c),
not `require()`. If TypeScript is used, it must be configured to
[output ES modules](https://www.typescriptlang.org/docs/handbook/esm-node.html),
not CommonJS.
# API
## getBinPath(options?)
[`options`](#options): `object`\
_Returns_: `Promise`
Returns the current package's binary absolute path. When no `package.json` or
binary can be found, `undefined` is returned instead.
```js
import { getBinPath } from 'get-bin-path'
const binPath = await getBinPath()
```
## getBinPathSync(options?)
[`options`](#options): `object`\
_Returns_: `string | undefined`
Same but synchronous.
```js
import { getBinPathSync } from 'get-bin-path'
const binPath = getBinPathSync()
```
### options
_Type_: `object`
#### options.name
_Type_: `string`\
_Default_: `package.json` `name` field
Name of the binary.
Only needs to be specified when the package exports several binaries in the
`package.json`
[`bin`](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#bin) or
[`directories.bin`](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#directoriesbin)
field.
```json
{
"name": "foo",
"bin": {
"foo": "path/to/foo.js",
"bar": "path/to/bar.js"
}
}
```
When the `package.json`'s `bin` or `directories.bin` field is a `string`, the
package
[`name` field](https://docs.npmjs.com/cli/v9/configuring-npm/package-json#name)
must match the `name` option.
#### options.cwd
_Type_: `string`\
_Default_: Current directory
Override the current directory, which is used when retrieving the
`package.json`.
# See also
- [`bin-path-cli`](https://github.com/tommy-mitchell/bin-path-cli): execute the
current package's binary
- [`execa`](https://github.com/sindresorhus/execa): process execution for humans
# Support
For any question, _don't hesitate_ to [submit an issue on GitHub](../../issues).
Everyone is welcome regardless of personal background. We enforce a
[Code of conduct](CODE_OF_CONDUCT.md) in order to promote a positive and
inclusive environment.
# Contributing
This project was made with ❤️. The simplest way to give back is by starring and
sharing it online.
If the documentation is unclear or has a typo, please click on the page's `Edit`
button (pencil icon) and suggest a correction.
If you would like to help us fix a bug or add a new feature, please check our
[guidelines](CONTRIBUTING.md). Pull requests are welcome!
Thanks go to our wonderful contributors:

ehmicky
💻 🎨 🤔 📖

Daniel Stockman
💻

Kabir Baidhya
💻

Minh Nguyen
🤔

Tyler Miller
🤔

Tommy
🤔 💻