Packages and scripts

Run package binaries with bunx

Use bunx to run package command-line tools without turning every temporary tool into a global installation.

6 minute lesson

~~~

Some npm packages provide command-line tools. bunx finds and runs those tools.

For example, run Prettier without installing it globally:

bunx prettier --check .

bunx first looks for the executable in the project’s installed packages. If it is missing, Bun downloads the package into a shared cache and runs it.

This is similar to npx.

Prefer local versions for project tools

A formatter used by a team should have a version recorded by the project:

bun add --dev prettier

Add a script to package.json:

{
  "scripts": {
    "format": "prettier --write .",
    "format:check": "prettier --check ."
  }
}

Now everyone runs the same installed version:

bun run format:check

Use bunx directly for temporary commands, project generators, or tools you are evaluating. Use a local development dependency for commands that belong to the project’s normal workflow.

Choose the runtime deliberately

Many package binaries start with a Node.js shebang. bunx respects it and uses Node.js.

You can force Bun with --bun:

bunx --bun vite --version

Do this only when you intend to test the tool under Bun. A command working through ordinary bunx does not prove the package itself ran on the Bun runtime.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →