Bun

bun run

The bun CLI can be used to execute JavaScript/TypeScript files, package.json scripts, and executable packages.

Performance

Bun is designed to start fast and run fast.

Under the hood Bun uses the JavaScriptCore engine, which is developed by Apple for Safari. In most cases, the startup and running performance is faster than V8, the engine used by Node.js and Chromium-based browsers. Its transpiler and runtime are written in Zig, a modern, high-performance language. On Linux, this translates into startup times 4x faster than Node.js.

bun hello.js5.2ms
node hello.js25.1ms
Running a simple Hello World script on Linux

Run a file

Compare to node <file>

Use bun run to execute a source file.

bun run index.js

Bun supports TypeScript and JSX out of the box. Every file is transpiled on the fly by Bun's fast native transpiler before being executed.

bun run index.js
bun run index.jsx
bun run index.ts
bun run index.tsx

Alternatively, you can omit the run keyword and use the "naked" command; it behaves identically.

bun index.tsx
bun index.js

--watch

To run a file in watch mode, use the --watch flag.

bun --watch run index.tsx

Note — When using bun run, put Bun flags like --watch immediately after bun.

bun --watch run dev # ✔️ do this
bun run dev --watch # ❌ don't do this

Flags that occur at the end of the command will be ignored and passed through to the "dev" script itself.

Run a package.json script

Compare to npm run <script> or yarn <script>

bun [bun flags] run <script> [script flags]

Your package.json can define a number of named "scripts" that correspond to shell commands.

{
  // ... other fields
  "scripts": {
    "clean": "rm -rf dist && echo 'Done.'",
    "dev": "bun server.ts"
  }
}

Use bun run <script> to execute these scripts.

bun run clean
 $ rm -rf dist && echo 'Done.'
 Cleaning...
 Done.

Bun executes the script command in a subshell. On Linux & macOS, it checks for the following shells in order, using the first one it finds: bash, sh, zsh. On windows, it uses bun shell to support bash-like syntax and many common commands.

⚡️ The startup time for npm run on Linux is roughly 170ms; with Bun it is 6ms.

Scripts can also be run with the shorter command bun <script>, however if there is a built-in bun command with the same name, the built-in command takes precedence. In this case, use the more explicit bun run <script> command to execute your package script.

bun run dev

To see a list of available scripts, run bun run without any arguments.

bun run
quickstart scripts:

 bun run clean
   rm -rf dist && echo 'Done.'

 bun run dev
   bun server.ts

2 scripts

Bun respects lifecycle hooks. For instance, bun run clean will execute preclean and postclean, if defined. If the pre<script> fails, Bun will not execute the script itself.

--bun

It's common for package.json scripts to reference locally-installed CLIs like vite or next. These CLIs are often JavaScript files marked with a shebang to indicate that they should be executed with node.

#!/usr/bin/env node

// do stuff

By default, Bun respects this shebang and executes the script with node. However, you can override this behavior with the --bun flag. For Node.js-based CLIs, this will run the CLI with Bun instead of Node.js.

bun run --bun vite

Filtering

In monorepos containing multiple packages, you can use the --filter argument to execute scripts in many packages at once.

Use bun run --filter <name_pattern> <script> to execute <script> in all packages whose name matches <name_pattern>. For example, if you have subdirectories containing packages named foo, bar and baz, running

bun run --filter 'ba*' <script>

will execute <script> in both bar and baz, but not in foo.

Find more details in the docs page for filter.

bun run - to pipe code from stdin

bun run - lets you read JavaScript, TypeScript, TSX, or JSX from stdin and execute it without writing to a temporary file first.

echo "console.log('Hello')" | bun run -
Hello

You can also use bun run - to redirect files into Bun. For example, to run a .js file as if it were a .ts file:

echo "console.log!('This is TypeScript!' as any)" > secretly-typescript.js
bun run - < secretly-typescript.js
This is TypeScript!

For convenience, all code is treated as TypeScript with JSX support when using bun run -.

bun run --smol

In memory-constrained environments, use the --smol flag to reduce memory usage at a cost to performance.

bun --smol run index.tsx

This causes the garbage collector to run more frequently, which can slow down execution. However, it can be useful in environments with limited memory. Bun automatically adjusts the garbage collector's heap size based on the available memory (accounting for cgroups and other memory limits) with and without the --smol flag, so this is mostly useful for cases where you want to make the heap size grow more slowly.

Resolution order

Absolute paths and paths starting with ./ or .\\ are always executed as source files. Unless using bun run, running a file with an allowed extension will prefer the file over a package.json script.

When there is a package.json script and a file with the same name, bun run prioritizes the package.json script. The full resolution order is:

  1. package.json scripts, eg bun run build
  2. Source files, eg bun run src/main.js
  3. Binaries from project packages, eg bun add eslint && bun run eslint
  4. (bun run only) System commands, eg bun run ls

CLI Usage

$bun run <file or script>

Flags

Execution

--silent
Don't print the script command
-b,--bun
Force a script or package to use Bun's runtime instead of Node.js (via symlinking node)
--watch
Automatically restart the process on file change
--hot
Enable auto reload in the Bun runtime, test runner, or bundler
--no-clear-screen
Disable clearing the terminal screen on reload when --hot or --watch is enabled
-e,--eval=<val>
Evaluate argument as a script
-p,--print=<val>
Evaluate argument as a script and print the result

Package Management

--no-install
Disable auto install in the Bun runtime
--install=<val>
Configure auto-install behavior. One of "auto" (default, auto-installs when no node_modules), "fallback" (missing packages only), "force" (always).
-i
Auto-install dependencies during execution. Equivalent to --install=fallback.
--prefer-offline
Skip staleness checks for packages in the Bun runtime and resolve from disk
--prefer-latest
Use the latest matching versions of packages in the Bun runtime, always checking npm

Debugging

--inspect=<val>
Activate Bun's debugger
--inspect-wait=<val>
Activate Bun's debugger, wait for a connection before executing
--inspect-brk=<val>
Activate Bun's debugger, set breakpoint on first line of code and wait

Environment

--shell=<val>
Control the shell used for package.json scripts. Supports either 'bun' or 'system'
--smol
Use less memory, but run garbage collection more often
--expose-gc
Expose gc() on the global object. Has no effect on Bun.gc().
--no-deprecation
Suppress all reporting of the custom deprecation.
--throw-deprecation
Determine whether or not deprecation warnings result in errors.
--title=<val>
Set the process title
--zero-fill-buffers
Boolean to force Buffer.allocUnsafe(size) to be zero-filled.
--main-fields=<val>
Main fields to lookup in package.json. Defaults to --target dependent
--extension-order=<val>
Defaults to: .tsx,.ts,.jsx,.js,.json

Configuration

--if-present
Exit without an error if the entrypoint does not exist
--port=<val>
Set the default port for Bun.serve
--conditions=<val>
Pass custom conditions to resolve
--fetch-preconnect=<val>
Preconnect to a URL while code is loading
--max-http-header-size=<val>
Set the maximum size of HTTP headers in bytes. Default is 16KiB
--dns-result-order=<val>
Set the default order of DNS lookup results. Valid orders: verbatim (default), ipv4first, ipv6first
--tsconfig-override=<val>
Specify custom tsconfig.json. Default <d>$cwd<r>/tsconfig.json
-c,--config=<val>
Specify path to Bun config file. Default <d>$cwd<r>/bunfig.toml

Filters and Execution Scope

--elide-lines=<val>
Number of lines of script output shown when using --filter (default: 10). Set to 0 to show all lines.
-F,--filter=<val>
Run a script in all workspace packages matching the pattern
-r,--preload=<val>
Import a module before other modules are loaded

Transpilation and Bundling

-d,--define=<val>
Substitute K:V while parsing, e.g. --define process.env.NODE_ENV:"development". Values are parsed as JSON.
--drop=<val>
Remove function calls, e.g. --drop=console removes all console.* calls.
-l,--loader=<val>
Parse files with .ext:loader, e.g. --loader .js:jsx. Valid loaders: js, jsx, ts, tsx, json, toml, text, file, wasm, napi
--no-macros
Disable macros from being executed in the bundler, transpiler and runtime
--jsx-factory=<val>
Changes the function called when compiling JSX elements using the classic JSX runtime
--jsx-fragment=<val>
Changes the function called when compiling JSX fragments
--jsx-import-source=<val>
Declares the module specifier to be used for importing the jsx and jsxs factory functions. Default: "react"
--jsx-runtime=<val>
"automatic" (default) or "classic"
--ignore-dce-annotations
Ignore tree-shaking annotations such as @__PURE__

Environment Variables

--env-file=<val>
Load environment variables from the specified file(s)
--cwd=<val>
Absolute path to resolve files & entry points from. This just changes the process' cwd.

Help and Errors

-h,--help
Display this menu and exit
--verbose-error-trace
Dump error return traces

Other Options

--breakpoint-print=<val>
DEBUG MODE: breakpoint when printing something that includes this string

Examples

Run a JavaScript or TypeScript file
bun run ./index.js
bun run ./index.tsx
Run a package.json script
bun run dev
bun run lint
Full documentation is available at https://bun.sh/docs/cli/run