| Command | Time |
|---|---|
bun hello.js | 5.2ms |
node hello.js | 25.1ms |
Running a file
Usebun run to execute a source file.
run keyword entirely. It behaves identically:
Reading from stdin
bun run - reads JavaScript, TypeScript, TSX, or JSX from stdin and executes it without writing a temporary file.
Watch mode
Use the--watch flag to automatically restart the process when files change.
When using
bun run, place Bun flags immediately after bun, not after the script name.--hot instead:
--watch and --hot.
Running package.json scripts
Bun can run scripts defined in yourpackage.json:
The startup time for
npm run on Linux is roughly 170ms; with Bun it is 6ms.bun run with no arguments:
bun run clean will execute preclean and postclean if they are defined. If the pre<script> step fails, the main script is not executed.
Running scripts with --bun
Package.json scripts often reference locally installed CLIs (such as vite or next) that include a #!/usr/bin/env node shebang. By default, Bun respects this and invokes them with node. Use --bun to run them with Bun instead:
Filtering in monorepos
In a monorepo with multiple packages, use--filter to run a script across all packages whose name matches a pattern:
build in every package whose name starts with ba.
Resolution order
When you runbun run <name>, Bun resolves in this order:
package.jsonscripts (e.g.,bun run build)- Source files (e.g.,
bun run src/main.js) - Binaries from project packages (e.g.,
bun run eslint) - System commands (e.g.,
bun run ls)
./ are always executed as source files.
Configuring the runtime with bunfig.toml
Bun’s runtime behavior can be configured with a bunfig.toml file in your project root. This file is optional — Bun works without it. For Bun-specific settings not covered by package.json or tsconfig.json, bunfig.toml is the right place.
Preloading scripts
Run scripts before your entry point executes. Useful for registering plugins or setting up test environments:JSX configuration
Configure JSX handling for non-TypeScript projects (TypeScript users can usetsconfig.json compilerOptions instead):
Reducing memory usage
Usesmol mode to reduce memory consumption at the cost of performance. Useful in memory-constrained environments:
--smol CLI flag:
Custom file loaders
Map file extensions to Bun loaders. For example, to treat.bagel files as TSX:
Console output depth
Control how deeply nested objects are printed byconsole.log. Default is 2:
--console-depth:
Environment variable loading
By default, Bun automatically loads.env files. To disable this:
--env-file are always loaded, regardless of this setting.
Global vs. local configuration
Placebunfig.toml in your project root alongside package.json. To configure Bun globally, create .bunfig.toml at $HOME/.bunfig.toml or $XDG_CONFIG_HOME/.bunfig.toml. When both exist, local configuration shallow-merges over global. CLI flags take precedence over both.
Environment variables
Bun automatically loads.env, .env.local, .env.production, and .env.development files. Variables are available via process.env.
import.meta.env, which is an alias for process.env: