Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: 3 days ago
JSON representation

Get the current package's binary path

Awesome Lists containing this project

README

        


get-bin-path logo

[![Node](https://img.shields.io/badge/-Node.js-808080?logo=node.js&colorA=404040&logoColor=66cc33)](https://www.npmjs.com/package/get-bin-path)
[![TypeScript](https://img.shields.io/badge/-Typed-808080?logo=typescript&colorA=404040&logoColor=0096ff)](/src/main.d.ts)
[![Codecov](https://img.shields.io/badge/-Tested%20100%25-808080?logo=codecov&colorA=404040)](https://codecov.io/gh/ehmicky/get-bin-path)
[![Mastodon](https://img.shields.io/badge/-Mastodon-808080.svg?logo=mastodon&colorA=404040&logoColor=9590F9)](https://fosstodon.org/@ehmicky)
[![Medium](https://img.shields.io/badge/-Medium-808080.svg?logo=medium&colorA=404040)](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

# Hire me

Please
[reach out](https://www.linkedin.com/feed/update/urn:li:activity:7117265228068716545/)
if you're looking for a Node.js API or CLI engineer (11 years of experience).
Most recently I have been [Netlify Build](https://github.com/netlify/build)'s
and [Netlify Plugins](https://www.netlify.com/products/build/plugins/)'
technical lead for 2.5 years. I am available for full-time remote positions.

# 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
ehmicky

💻 🎨 🤔 📖
Daniel Stockman
Daniel Stockman

💻
Kabir Baidhya
Kabir Baidhya

💻
Minh Nguyen
Minh Nguyen

🤔
Tyler Miller
Tyler Miller

🤔
Tommy
Tommy

🤔 💻