* Docs & types for 0.7 * Tweak * Update * Tweaks * Tweak * Tweaks --------- Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
4.1 KiB
The bun CLI can be used to execute JavaScript/TypeScript files, package.json scripts, and executable packages.
Run a file
{% callout %}
Compare to node <file>
{% /callout %}
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
The "naked" bun command is equivalent to bun run.
$ bun index.tsx
--watch
To run a file in watch mode, use the --watch flag.
$ bun --watch run index.tsx
--smol
{% callout %} Added in Bun v0.7.0. {% /callout %}
In memory-constrained environments, use the --smol flag to reduce memory usage at a cost to performance.
$ bun --smol run index.tsx
Run a package.json script
{% note %}
Compare to npm run <script> or yarn <script>
{% /note %}
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 <script> to execute these scripts.
$ bun clean
$ rm -rf dist && echo 'Done.'
Cleaning...
Done.
Bun executes the script command in a subshell. It checks for the following shells in order, using the first one it finds: bash, sh, zsh.
{% callout %}
⚡️ The startup time for npm run on Linux is roughly 170ms; with Bun it is 6ms.
{% /callout %}
If there is a name conflict between a package.json script and a built-in bun command (install, dev, upgrade, etc.) Bun's built-in command takes precedence. In this case, use the more explicit bun run 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.
Environment variables
Bun automatically loads environment variables from .env files before running a file, script, or executable. The following files are checked, in order:
.env.local(first)NODE_ENV==="production"?.env.production:.env.development.env
To debug environment variables, run bun run env to view a list of resolved environment variables.
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.
{% image src="/images/bun-run-speed.jpeg" caption="Bun vs Node.js vs Deno running Hello World" /%}