包详细信息

get-bin-path

ehmicky16.9kApache-2.011.0.1

Get the current package's binary path

binary, package-json, npm, path

自述文件

<picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ehmicky/design/main/get-bin-path/get-bin-path_dark.svg"/> get-bin-path logo </picture>

Node TypeScript Codecov Mastodon Medium

Get the current package's binary path (using the package.json bin or directories.bin 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

import { getBinPath } from 'get-bin-path'

// `binPath` is the absolute path to the current package's binary
const binPath = await getBinPath()
// 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, not require(). If TypeScript is used, it must be configured to output ES modules, not CommonJS.

API

getBinPath(options?)

options: object\ Returns: Promise<string | undefined>

Returns the current package's binary absolute path. When no package.json or binary can be found, undefined is returned instead.

import { getBinPath } from 'get-bin-path'

const binPath = await getBinPath()

getBinPathSync(options?)

options: object\ Returns: string | undefined

Same but synchronous.

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 or directories.bin field.

{
  "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 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: execute the current package's binary
  • execa: process execution for humans

Support

For any question, don't hesitate to submit an issue on GitHub.

Everyone is welcome regardless of personal background. We enforce a Code of conduct 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. 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

🤔 💻

更新日志

11.0.1

Documentation

  • Improve documentation in README.md

11.0.0

Breaking changes

  • Minimal supported Node.js version is now 18.18.0

10.0.0

Breaking changes

  • Minimal supported Node.js version is now 16.17.0

9.0.0

Breaking changes

  • undefined is now always returned when the binary cannot be found. Previously, a random binary was sometimes returned instead. This happens when:
  • When the package.json bin field is a string, the name option (when defined) must match the package.json name field.

8.0.0

Breaking changes

7.3.0

Features

  • Reduce package size by 96% (764kB to 31kB)
  • Improve performance

7.2.1

Bug fixes

  • Fix package.json

7.2.0

Features

  • Improve tree-shaking support

7.1.0

Features

  • Reduce npm package size

7.0.0

Breaking changes

  • Minimal supported Node.js version is now 14.18.0

6.1.0

Features

  • Improve TypeScript types

6.0.1

Bug fixes

  • Fix main field in package.json

6.0.0

Breaking changes

  • Minimal supported Node.js version is now 12.20.0
  • This package is now an ES module. It can only be loaded with an import or import() statement, not require(). See this post for more information.

5.1.0

Features

  • Add TypeScript types

5.0.1

Dependencies

  • Remove core-js dependency

5.0.0

Breaking changes

  • Minimal supported Node.js version is now 10.17.0

4.0.1 (backport)

Dependencies

  • Remove core-js dependency

4.0.0

Breaking changes

  • The name argument must now be specified as an option instead. For example getBinPath(name, { cwd }) is now getBinPath({ name, cwd }).

3.0.0

Features

  • When the package.json bin field is an object with a single property (such as { "bin": { "mylib": "./path/to/bin.js" } }), it is not longer necessary to specify the binary name.