bun-types 1.3.2-canary.20251106T140813 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bun.d.ts +102 -6
- package/docs/bundler/bytecode.mdx +465 -0
- package/docs/bundler/css.mdx +1024 -0
- package/docs/bundler/esbuild.mdx +253 -0
- package/docs/bundler/executables.mdx +535 -0
- package/docs/bundler/fullstack.mdx +1064 -0
- package/docs/bundler/hot-reloading.mdx +229 -0
- package/docs/bundler/html-static.mdx +386 -0
- package/docs/bundler/index.mdx +1499 -0
- package/docs/bundler/loaders.mdx +356 -0
- package/docs/bundler/macros.mdx +328 -0
- package/docs/bundler/minifier.mdx +1306 -0
- package/docs/bundler/plugins.mdx +411 -0
- package/docs/feedback.mdx +85 -0
- package/docs/guides/binary/arraybuffer-to-array.mdx +29 -0
- package/docs/guides/binary/arraybuffer-to-blob.mdx +26 -0
- package/docs/guides/binary/arraybuffer-to-buffer.mdx +27 -0
- package/docs/guides/binary/arraybuffer-to-string.mdx +17 -0
- package/docs/guides/binary/arraybuffer-to-typedarray.mdx +41 -0
- package/docs/guides/binary/blob-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/blob-to-dataview.mdx +16 -0
- package/docs/guides/binary/blob-to-stream.mdx +16 -0
- package/docs/guides/binary/blob-to-string.mdx +17 -0
- package/docs/guides/binary/blob-to-typedarray.mdx +16 -0
- package/docs/guides/binary/buffer-to-arraybuffer.mdx +16 -0
- package/docs/guides/binary/buffer-to-blob.mdx +16 -0
- package/docs/guides/binary/buffer-to-readablestream.mdx +43 -0
- package/docs/guides/binary/buffer-to-string.mdx +27 -0
- package/docs/guides/binary/buffer-to-typedarray.mdx +16 -0
- package/docs/guides/binary/dataview-to-string.mdx +17 -0
- package/docs/guides/binary/typedarray-to-arraybuffer.mdx +27 -0
- package/docs/guides/binary/typedarray-to-blob.mdx +18 -0
- package/docs/guides/binary/typedarray-to-buffer.mdx +16 -0
- package/docs/guides/binary/typedarray-to-dataview.mdx +16 -0
- package/docs/guides/binary/typedarray-to-readablestream.mdx +43 -0
- package/docs/guides/binary/typedarray-to-string.mdx +18 -0
- package/docs/guides/deployment/aws-lambda.mdx +204 -0
- package/docs/guides/deployment/digital-ocean.mdx +161 -0
- package/docs/guides/deployment/google-cloud-run.mdx +197 -0
- package/docs/guides/deployment/railway.mdx +145 -0
- package/docs/guides/deployment/render.mdx +82 -0
- package/docs/guides/deployment/vercel.mdx +99 -0
- package/docs/guides/ecosystem/astro.mdx +82 -0
- package/docs/guides/ecosystem/discordjs.mdx +80 -0
- package/docs/guides/ecosystem/docker.mdx +151 -0
- package/docs/guides/ecosystem/drizzle.mdx +195 -0
- package/docs/guides/ecosystem/edgedb.mdx +257 -0
- package/docs/guides/ecosystem/elysia.mdx +31 -0
- package/docs/guides/ecosystem/express.mdx +43 -0
- package/docs/guides/ecosystem/hono.mdx +47 -0
- package/docs/guides/ecosystem/mongoose.mdx +92 -0
- package/docs/guides/ecosystem/neon-drizzle.mdx +234 -0
- package/docs/guides/ecosystem/neon-serverless-postgres.mdx +60 -0
- package/docs/guides/ecosystem/nextjs.mdx +57 -0
- package/docs/guides/ecosystem/nuxt.mdx +90 -0
- package/docs/guides/ecosystem/pm2.mdx +55 -0
- package/docs/guides/ecosystem/prisma-postgres.mdx +169 -0
- package/docs/guides/ecosystem/prisma.mdx +164 -0
- package/docs/guides/ecosystem/qwik.mdx +114 -0
- package/docs/guides/ecosystem/react.mdx +52 -0
- package/docs/guides/ecosystem/remix.mdx +97 -0
- package/docs/guides/ecosystem/sentry.mdx +54 -0
- package/docs/guides/ecosystem/solidstart.mdx +66 -0
- package/docs/guides/ecosystem/ssr-react.mdx +49 -0
- package/docs/guides/ecosystem/stric.mdx +54 -0
- package/docs/guides/ecosystem/sveltekit.mdx +138 -0
- package/docs/guides/ecosystem/systemd.mdx +114 -0
- package/docs/guides/ecosystem/upstash.mdx +87 -0
- package/docs/guides/ecosystem/vite.mdx +77 -0
- package/docs/guides/html-rewriter/extract-links.mdx +72 -0
- package/docs/guides/html-rewriter/extract-social-meta.mdx +97 -0
- package/docs/guides/http/cluster.mdx +69 -0
- package/docs/guides/http/fetch-unix.mdx +35 -0
- package/docs/guides/http/fetch.mdx +26 -0
- package/docs/guides/http/file-uploads.mdx +97 -0
- package/docs/guides/http/hot.mdx +28 -0
- package/docs/guides/http/proxy.mdx +26 -0
- package/docs/guides/http/server.mdx +48 -0
- package/docs/guides/http/simple.mdx +20 -0
- package/docs/guides/http/stream-file.mdx +50 -0
- package/docs/guides/http/stream-iterator.mdx +49 -0
- package/docs/guides/http/stream-node-streams-in-bun.mdx +22 -0
- package/docs/guides/http/tls.mdx +32 -0
- package/docs/guides/index.mdx +10 -0
- package/docs/guides/install/add-dev.mdx +28 -0
- package/docs/guides/install/add-git.mdx +38 -0
- package/docs/guides/install/add-optional.mdx +27 -0
- package/docs/guides/install/add-peer.mdx +45 -0
- package/docs/guides/install/add-tarball.mdx +35 -0
- package/docs/guides/install/add.mdx +44 -0
- package/docs/guides/install/azure-artifacts.mdx +76 -0
- package/docs/guides/install/cicd.mdx +43 -0
- package/docs/guides/install/custom-registry.mdx +32 -0
- package/docs/guides/install/from-npm-install-to-bun-install.mdx +230 -0
- package/docs/guides/install/git-diff-bun-lockfile.mdx +47 -0
- package/docs/guides/install/jfrog-artifactory.mdx +28 -0
- package/docs/guides/install/npm-alias.mdx +25 -0
- package/docs/guides/install/registry-scope.mdx +40 -0
- package/docs/guides/install/trusted.mdx +50 -0
- package/docs/guides/install/workspaces.mdx +70 -0
- package/docs/guides/install/yarnlock.mdx +50 -0
- package/docs/guides/process/argv.mdx +66 -0
- package/docs/guides/process/ctrl-c.mdx +18 -0
- package/docs/guides/process/ipc.mdx +69 -0
- package/docs/guides/process/nanoseconds.mdx +15 -0
- package/docs/guides/process/os-signals.mdx +41 -0
- package/docs/guides/process/spawn-stderr.mdx +34 -0
- package/docs/guides/process/spawn-stdout.mdx +28 -0
- package/docs/guides/process/spawn.mdx +43 -0
- package/docs/guides/process/stdin.mdx +62 -0
- package/docs/guides/read-file/arraybuffer.mdx +30 -0
- package/docs/guides/read-file/buffer.mdx +21 -0
- package/docs/guides/read-file/exists.mdx +18 -0
- package/docs/guides/read-file/json.mdx +19 -0
- package/docs/guides/read-file/mime.mdx +22 -0
- package/docs/guides/read-file/stream.mdx +28 -0
- package/docs/guides/read-file/string.mdx +24 -0
- package/docs/guides/read-file/uint8array.mdx +23 -0
- package/docs/guides/read-file/watch.mdx +66 -0
- package/docs/guides/runtime/build-time-constants.mdx +295 -0
- package/docs/guides/runtime/cicd.mdx +45 -0
- package/docs/guides/runtime/codesign-macos-executable.mdx +61 -0
- package/docs/guides/runtime/define-constant.mdx +149 -0
- package/docs/guides/runtime/delete-directory.mdx +39 -0
- package/docs/guides/runtime/delete-file.mdx +21 -0
- package/docs/guides/runtime/heap-snapshot.mdx +28 -0
- package/docs/guides/runtime/import-html.mdx +17 -0
- package/docs/guides/runtime/import-json.mdx +46 -0
- package/docs/guides/runtime/import-toml.mdx +32 -0
- package/docs/guides/runtime/import-yaml.mdx +104 -0
- package/docs/guides/runtime/read-env.mdx +37 -0
- package/docs/guides/runtime/set-env.mdx +51 -0
- package/docs/guides/runtime/shell.mdx +42 -0
- package/docs/guides/runtime/timezone.mdx +38 -0
- package/docs/guides/runtime/tsconfig-paths.mdx +31 -0
- package/docs/guides/runtime/typescript.mdx +51 -0
- package/docs/guides/runtime/vscode-debugger.mdx +48 -0
- package/docs/guides/runtime/web-debugger.mdx +103 -0
- package/docs/guides/streams/node-readable-to-arraybuffer.mdx +13 -0
- package/docs/guides/streams/node-readable-to-blob.mdx +13 -0
- package/docs/guides/streams/node-readable-to-json.mdx +14 -0
- package/docs/guides/streams/node-readable-to-string.mdx +14 -0
- package/docs/guides/streams/node-readable-to-uint8array.mdx +13 -0
- package/docs/guides/streams/to-array.mdx +16 -0
- package/docs/guides/streams/to-arraybuffer.mdx +16 -0
- package/docs/guides/streams/to-blob.mdx +16 -0
- package/docs/guides/streams/to-buffer.mdx +17 -0
- package/docs/guides/streams/to-json.mdx +16 -0
- package/docs/guides/streams/to-string.mdx +16 -0
- package/docs/guides/streams/to-typedarray.mdx +24 -0
- package/docs/guides/test/bail.mdx +24 -0
- package/docs/guides/test/coverage-threshold.mdx +67 -0
- package/docs/guides/test/coverage.mdx +49 -0
- package/docs/guides/test/happy-dom.mdx +73 -0
- package/docs/guides/test/migrate-from-jest.mdx +125 -0
- package/docs/guides/test/mock-clock.mdx +50 -0
- package/docs/guides/test/mock-functions.mdx +70 -0
- package/docs/guides/test/rerun-each.mdx +16 -0
- package/docs/guides/test/run-tests.mdx +116 -0
- package/docs/guides/test/skip-tests.mdx +43 -0
- package/docs/guides/test/snapshot.mdx +102 -0
- package/docs/guides/test/spy-on.mdx +49 -0
- package/docs/guides/test/svelte-test.mdx +113 -0
- package/docs/guides/test/testing-library.mdx +93 -0
- package/docs/guides/test/timeout.mdx +17 -0
- package/docs/guides/test/todo-tests.mdx +74 -0
- package/docs/guides/test/update-snapshots.mdx +49 -0
- package/docs/guides/test/watch-mode.mdx +24 -0
- package/docs/guides/util/base64.mdx +17 -0
- package/docs/guides/util/deep-equals.mdx +41 -0
- package/docs/guides/util/deflate.mdx +20 -0
- package/docs/guides/util/detect-bun.mdx +25 -0
- package/docs/guides/util/entrypoint.mdx +19 -0
- package/docs/guides/util/escape-html.mdx +24 -0
- package/docs/guides/util/file-url-to-path.mdx +16 -0
- package/docs/guides/util/gzip.mdx +20 -0
- package/docs/guides/util/hash-a-password.mdx +56 -0
- package/docs/guides/util/import-meta-dir.mdx +15 -0
- package/docs/guides/util/import-meta-file.mdx +15 -0
- package/docs/guides/util/import-meta-path.mdx +15 -0
- package/docs/guides/util/javascript-uuid.mdx +25 -0
- package/docs/guides/util/main.mdx +43 -0
- package/docs/guides/util/path-to-file-url.mdx +16 -0
- package/docs/guides/util/sleep.mdx +24 -0
- package/docs/guides/util/version.mdx +23 -0
- package/docs/guides/util/which-path-to-executable-bin.mdx +17 -0
- package/docs/guides/websocket/compression.mdx +33 -0
- package/docs/guides/websocket/context.mdx +74 -0
- package/docs/guides/websocket/pubsub.mdx +40 -0
- package/docs/guides/websocket/simple.mdx +35 -0
- package/docs/guides/write-file/append.mdx +54 -0
- package/docs/guides/write-file/basic.mdx +46 -0
- package/docs/guides/write-file/blob.mdx +30 -0
- package/docs/guides/write-file/cat.mdx +19 -0
- package/docs/guides/write-file/file-cp.mdx +18 -0
- package/docs/guides/write-file/filesink.mdx +54 -0
- package/docs/guides/write-file/response.mdx +19 -0
- package/docs/guides/write-file/stdout.mdx +23 -0
- package/docs/guides/write-file/stream.mdx +19 -0
- package/docs/guides/write-file/unlink.mdx +18 -0
- package/docs/index.mdx +133 -0
- package/docs/installation.mdx +365 -0
- package/docs/pm/bunx.mdx +83 -0
- package/docs/pm/catalogs.mdx +292 -0
- package/docs/pm/cli/add.mdx +179 -0
- package/docs/pm/cli/audit.mdx +60 -0
- package/docs/pm/cli/install.mdx +471 -0
- package/docs/pm/cli/link.mdx +48 -0
- package/docs/pm/cli/outdated.mdx +197 -0
- package/docs/pm/cli/patch.mdx +69 -0
- package/docs/pm/cli/pm.mdx +319 -0
- package/docs/pm/cli/publish.mdx +123 -0
- package/docs/pm/cli/remove.mdx +16 -0
- package/docs/pm/cli/update.mdx +140 -0
- package/docs/pm/cli/why.mdx +84 -0
- package/docs/pm/filter.mdx +102 -0
- package/docs/pm/global-cache.mdx +72 -0
- package/docs/pm/isolated-installs.mdx +205 -0
- package/docs/pm/lifecycle.mdx +57 -0
- package/docs/pm/lockfile.mdx +64 -0
- package/docs/pm/npmrc.mdx +111 -0
- package/docs/pm/overrides.mdx +83 -0
- package/docs/pm/scopes-registries.mdx +35 -0
- package/docs/pm/security-scanner-api.mdx +95 -0
- package/docs/pm/workspaces.mdx +109 -0
- package/docs/project/benchmarking.mdx +218 -0
- package/docs/project/bindgen.mdx +223 -0
- package/docs/project/building-windows.mdx +133 -0
- package/docs/project/contributing.mdx +343 -0
- package/docs/project/feedback.mdx +20 -0
- package/docs/project/license.mdx +78 -0
- package/docs/project/roadmap.mdx +8 -0
- package/docs/quickstart.mdx +240 -0
- package/docs/runtime/auto-install.mdx +97 -0
- package/docs/runtime/binary-data.mdx +846 -0
- package/docs/runtime/bun-apis.mdx +59 -0
- package/docs/runtime/bunfig.mdx +642 -0
- package/docs/runtime/c-compiler.mdx +204 -0
- package/docs/runtime/child-process.mdx +532 -0
- package/docs/runtime/color.mdx +267 -0
- package/docs/runtime/console.mdx +67 -0
- package/docs/runtime/cookies.mdx +454 -0
- package/docs/runtime/debugger.mdx +335 -0
- package/docs/runtime/environment-variables.mdx +214 -0
- package/docs/runtime/ffi.mdx +565 -0
- package/docs/runtime/file-io.mdx +306 -0
- package/docs/runtime/file-system-router.mdx +118 -0
- package/docs/runtime/file-types.mdx +354 -0
- package/docs/runtime/glob.mdx +181 -0
- package/docs/runtime/globals.mdx +72 -0
- package/docs/runtime/hashing.mdx +315 -0
- package/docs/runtime/html-rewriter.mdx +340 -0
- package/docs/runtime/http/cookies.mdx +79 -0
- package/docs/runtime/http/error-handling.mdx +40 -0
- package/docs/runtime/http/metrics.mdx +36 -0
- package/docs/runtime/http/routing.mdx +289 -0
- package/docs/runtime/http/server.mdx +647 -0
- package/docs/runtime/http/tls.mdx +101 -0
- package/docs/runtime/http/websockets.mdx +404 -0
- package/docs/runtime/index.mdx +223 -0
- package/docs/runtime/jsx.mdx +115 -0
- package/docs/runtime/module-resolution.mdx +342 -0
- package/docs/runtime/networking/dns.mdx +111 -0
- package/docs/runtime/networking/fetch.mdx +468 -0
- package/docs/runtime/networking/tcp.mdx +239 -0
- package/docs/runtime/networking/udp.mdx +129 -0
- package/docs/runtime/node-api.mdx +19 -0
- package/docs/runtime/nodejs-compat.mdx +468 -0
- package/docs/runtime/plugins.mdx +405 -0
- package/docs/runtime/redis.mdx +582 -0
- package/docs/runtime/s3.mdx +863 -0
- package/docs/runtime/secrets.mdx +336 -0
- package/docs/runtime/semver.mdx +57 -0
- package/docs/runtime/shell.mdx +637 -0
- package/docs/runtime/sql.mdx +1404 -0
- package/docs/runtime/sqlite.mdx +699 -0
- package/docs/runtime/streams.mdx +232 -0
- package/docs/runtime/templating/create.mdx +269 -0
- package/docs/runtime/templating/init.mdx +58 -0
- package/docs/runtime/transpiler.mdx +288 -0
- package/docs/runtime/typescript.mdx +58 -0
- package/docs/runtime/utils.mdx +922 -0
- package/docs/runtime/watch-mode.mdx +161 -0
- package/docs/runtime/web-apis.mdx +29 -0
- package/docs/runtime/workers.mdx +328 -0
- package/docs/runtime/yaml.mdx +469 -0
- package/docs/snippets/cli/add.mdx +166 -0
- package/docs/snippets/cli/build.mdx +196 -0
- package/docs/snippets/cli/feedback.mdx +17 -0
- package/docs/snippets/cli/init.mdx +84 -0
- package/docs/snippets/cli/install.mdx +173 -0
- package/docs/snippets/cli/link.mdx +163 -0
- package/docs/snippets/cli/outdated.mdx +140 -0
- package/docs/snippets/cli/patch.mdx +171 -0
- package/docs/snippets/cli/publish.mdx +198 -0
- package/docs/snippets/cli/remove.mdx +146 -0
- package/docs/snippets/cli/run.mdx +293 -0
- package/docs/snippets/cli/test.mdx +100 -0
- package/docs/snippets/cli/update.mdx +144 -0
- package/docs/snippets/product-card.mdx +32 -0
- package/docs/snippets/product-tiles.mdx +94 -0
- package/docs/test/code-coverage.mdx +409 -0
- package/docs/test/configuration.mdx +467 -0
- package/docs/test/dates-times.mdx +129 -0
- package/docs/test/discovery.mdx +90 -0
- package/docs/test/dom.mdx +226 -0
- package/docs/test/index.mdx +380 -0
- package/docs/test/lifecycle.mdx +348 -0
- package/docs/test/mocks.mdx +637 -0
- package/docs/test/reporters.mdx +117 -0
- package/docs/test/runtime-behavior.mdx +342 -0
- package/docs/test/snapshots.mdx +434 -0
- package/docs/test/writing-tests.mdx +635 -0
- package/docs/typescript.mdx +54 -0
- package/package.json +8 -6
- package/test.d.ts +2 -2
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Bun Runtime
|
|
3
|
+
description: Execute JavaScript/TypeScript files, package.json scripts, and executable packages with Bun's fast runtime.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import Run from "/snippets/cli/run.mdx";
|
|
7
|
+
|
|
8
|
+
The Bun Runtime is designed to start fast and run fast.
|
|
9
|
+
|
|
10
|
+
Under the hood, Bun uses the [JavaScriptCore engine](https://developer.apple.com/documentation/javascriptcore), 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](https://twitter.com/jarredsumner/status/1499225725492076544) than Node.js.
|
|
11
|
+
|
|
12
|
+
| Command | Time |
|
|
13
|
+
| --------------- | -------- |
|
|
14
|
+
| `bun hello.js` | `5.2ms` |
|
|
15
|
+
| `node hello.js` | `25.1ms` |
|
|
16
|
+
|
|
17
|
+
This benchmark is based on running a simple Hello World script on Linux
|
|
18
|
+
|
|
19
|
+
## Run a file
|
|
20
|
+
|
|
21
|
+
Use `bun run` to execute a source file.
|
|
22
|
+
|
|
23
|
+
```bash terminal icon="terminal"
|
|
24
|
+
bun run index.js
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
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.
|
|
28
|
+
|
|
29
|
+
```bash terminal icon="terminal"
|
|
30
|
+
bun run index.js
|
|
31
|
+
bun run index.jsx
|
|
32
|
+
bun run index.ts
|
|
33
|
+
bun run index.tsx
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Alternatively, you can omit the `run` keyword and use the "naked" command; it behaves identically.
|
|
37
|
+
|
|
38
|
+
```bash terminal icon="terminal"
|
|
39
|
+
bun index.tsx
|
|
40
|
+
bun index.js
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### `--watch`
|
|
44
|
+
|
|
45
|
+
To run a file in watch mode, use the `--watch` flag.
|
|
46
|
+
|
|
47
|
+
```bash terminal icon="terminal"
|
|
48
|
+
bun --watch run index.tsx
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
<Note>
|
|
52
|
+
When using `bun run`, put Bun flags like `--watch` immediately after `bun`.
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
bun --watch run dev # ✔️ do this
|
|
56
|
+
bun run dev --watch # ❌ don't do this
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Flags that occur at the end of the command will be ignored and passed through to the `"dev"` script itself.
|
|
60
|
+
|
|
61
|
+
</Note>
|
|
62
|
+
|
|
63
|
+
## Run a `package.json` script
|
|
64
|
+
|
|
65
|
+
<Note>
|
|
66
|
+
Compare to `npm run <script>` or `yarn <script>`
|
|
67
|
+
</Note>
|
|
68
|
+
|
|
69
|
+
```sh
|
|
70
|
+
bun [bun flags] run <script> [script flags]
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Your `package.json` can define a number of named `"scripts"` that correspond to shell commands.
|
|
74
|
+
|
|
75
|
+
```json package.json icon="file-json"
|
|
76
|
+
{
|
|
77
|
+
// ... other fields
|
|
78
|
+
"scripts": {
|
|
79
|
+
"clean": "rm -rf dist && echo 'Done.'",
|
|
80
|
+
"dev": "bun server.ts"
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Use `bun run <script>` to execute these scripts.
|
|
86
|
+
|
|
87
|
+
```bash terminal icon="terminal"
|
|
88
|
+
bun run clean
|
|
89
|
+
rm -rf dist && echo 'Done.'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
```txt
|
|
93
|
+
Cleaning...
|
|
94
|
+
Done.
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
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](https://bun.com/docs/runtime/shell) to support bash-like syntax and many common commands.
|
|
98
|
+
|
|
99
|
+
<Note>⚡️ The startup time for `npm run` on Linux is roughly 170ms; with Bun it is `6ms`.</Note>
|
|
100
|
+
|
|
101
|
+
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.
|
|
102
|
+
|
|
103
|
+
```bash terminal icon="terminal"
|
|
104
|
+
bun run dev
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
To see a list of available scripts, run `bun run` without any arguments.
|
|
108
|
+
|
|
109
|
+
```bash terminal icon="terminal"
|
|
110
|
+
bun run
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```txt
|
|
114
|
+
quickstart scripts:
|
|
115
|
+
|
|
116
|
+
bun run clean
|
|
117
|
+
rm -rf dist && echo 'Done.'
|
|
118
|
+
|
|
119
|
+
bun run dev
|
|
120
|
+
bun server.ts
|
|
121
|
+
|
|
122
|
+
2 scripts
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
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.
|
|
126
|
+
|
|
127
|
+
### `--bun`
|
|
128
|
+
|
|
129
|
+
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](<https://en.wikipedia.org/wiki/Shebang_(Unix)>) to indicate that they should be executed with `node`.
|
|
130
|
+
|
|
131
|
+
```js cli.js icon="/icons/javascript.svg"
|
|
132
|
+
#!/usr/bin/env node
|
|
133
|
+
|
|
134
|
+
// do stuff
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
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.
|
|
138
|
+
|
|
139
|
+
```bash terminal icon="terminal"
|
|
140
|
+
bun run --bun vite
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Filtering
|
|
144
|
+
|
|
145
|
+
In monorepos containing multiple packages, you can use the `--filter` argument to execute scripts in many packages at once.
|
|
146
|
+
|
|
147
|
+
Use `bun run --filter <name_pattern> <script>` to execute `<script>` in all packages whose name matches `<name_pattern>`.
|
|
148
|
+
For example, if you have subdirectories containing packages named `foo`, `bar` and `baz`, running
|
|
149
|
+
|
|
150
|
+
```bash terminal icon="terminal"
|
|
151
|
+
bun run --filter 'ba*' <script>
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
will execute `<script>` in both `bar` and `baz`, but not in `foo`.
|
|
155
|
+
|
|
156
|
+
Find more details in the docs page for [filter](https://bun.com/docs/cli/filter#running-scripts-with-filter).
|
|
157
|
+
|
|
158
|
+
## `bun run -` to pipe code from stdin
|
|
159
|
+
|
|
160
|
+
`bun run -` lets you read JavaScript, TypeScript, TSX, or JSX from stdin and execute it without writing to a temporary file first.
|
|
161
|
+
|
|
162
|
+
```bash terminal icon="terminal"
|
|
163
|
+
echo "console.log('Hello')" | bun run -
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
```txt
|
|
167
|
+
Hello
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
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:
|
|
171
|
+
|
|
172
|
+
```bash terminal icon="terminal"
|
|
173
|
+
echo "console.log!('This is TypeScript!' as any)" > secretly-typescript.js
|
|
174
|
+
bun run - < secretly-typescript.js
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
```txt
|
|
178
|
+
This is TypeScript!
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
For convenience, all code is treated as TypeScript with JSX support when using `bun run -`.
|
|
182
|
+
|
|
183
|
+
## `bun run --console-depth`
|
|
184
|
+
|
|
185
|
+
Control the depth of object inspection in console output with the `--console-depth` flag.
|
|
186
|
+
|
|
187
|
+
```bash terminal icon="terminal"
|
|
188
|
+
bun --console-depth 5 run index.tsx
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
This sets how deeply nested objects are displayed in `console.log()` output. The default depth is `2`. Higher values show more nested properties but may produce verbose output for complex objects.
|
|
192
|
+
|
|
193
|
+
```ts console.ts icon="/icons/typescript.svg"
|
|
194
|
+
const nested = { a: { b: { c: { d: "deep" } } } };
|
|
195
|
+
console.log(nested);
|
|
196
|
+
// With --console-depth 2 (default): { a: { b: [Object] } }
|
|
197
|
+
// With --console-depth 4: { a: { b: { c: { d: 'deep' } } } }
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## `bun run --smol`
|
|
201
|
+
|
|
202
|
+
In memory-constrained environments, use the `--smol` flag to reduce memory usage at a cost to performance.
|
|
203
|
+
|
|
204
|
+
```bash terminal icon="terminal"
|
|
205
|
+
bun --smol run index.tsx
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
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.
|
|
209
|
+
|
|
210
|
+
## Resolution order
|
|
211
|
+
|
|
212
|
+
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.
|
|
213
|
+
|
|
214
|
+
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:
|
|
215
|
+
|
|
216
|
+
1. package.json scripts, eg `bun run build`
|
|
217
|
+
2. Source files, eg `bun run src/main.js`
|
|
218
|
+
3. Binaries from project packages, eg `bun add eslint && bun run eslint`
|
|
219
|
+
4. (`bun run` only) System commands, eg `bun run ls`
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
<Run />
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "JSX"
|
|
3
|
+
description: "Built-in JSX and TSX support in Bun with configurable transpilation options"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Bun supports `.jsx` and `.tsx` files out of the box. Bun's internal transpiler converts JSX syntax into vanilla JavaScript before execution.
|
|
7
|
+
|
|
8
|
+
```ts react.tsx icon="/icons/typescript.svg"
|
|
9
|
+
function Component(props: {message: string}) {
|
|
10
|
+
return (
|
|
11
|
+
<body>
|
|
12
|
+
<h1 style={{color: 'red'}}>{props.message}</h1>
|
|
13
|
+
</body>
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
console.log(<Component message="Hello world!" />);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Configuration
|
|
21
|
+
|
|
22
|
+
Bun reads your `tsconfig.json` or `jsconfig.json` configuration files to determines how to perform the JSX transform internally. To avoid using either of these, the following options can also be defined in [`bunfig.toml`](/runtime/bunfig).
|
|
23
|
+
|
|
24
|
+
The following compiler options are respected.
|
|
25
|
+
|
|
26
|
+
### [`jsx`](https://www.typescriptlang.org/tsconfig#jsx)
|
|
27
|
+
|
|
28
|
+
How JSX constructs are transformed into vanilla JavaScript internally. The table below lists the possible values of `jsx`, along with their transpilation of the following simple JSX component:
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
<Box width={5}>Hello</Box>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
| Compiler options | Transpiled output |
|
|
35
|
+
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
36
|
+
| `json<br/>{<br/> "jsx": "react"<br/>}<br/>` | `tsx<br/>import { createElement } from "react";<br/>createElement("Box", { width: 5 }, "Hello");<br/>` |
|
|
37
|
+
| `json<br/>{<br/> "jsx": "react-jsx"<br/>}<br/>` | `tsx<br/>import { jsx } from "react/jsx-runtime";<br/>jsx("Box", { width: 5 }, "Hello");<br/>` |
|
|
38
|
+
| `json<br/>{<br/> "jsx": "react-jsxdev"<br/>}<br/>` | `tsx<br/>import { jsxDEV } from "react/jsx-dev-runtime";<br/>jsxDEV(<br/> "Box",<br/> { width: 5, children: "Hello" },<br/> undefined,<br/> false,<br/> undefined,<br/> this,<br/>);<br/>`<br/><br/>The `jsxDEV` variable name is a convention used by React. The `DEV` suffix is a visible way to indicate that the code is intended for use in development. The development version of React is slower and includes additional validity checks & debugging tools. |
|
|
39
|
+
| `json<br/>{<br/> "jsx": "preserve"<br/>}<br/>` | `tsx<br/>// JSX is not transpiled<br/>// "preserve" is not supported by Bun currently<br/><Box width={5}>Hello</Box><br/>` |
|
|
40
|
+
|
|
41
|
+
### [`jsxFactory`](https://www.typescriptlang.org/tsconfig#jsxFactory)
|
|
42
|
+
|
|
43
|
+
<Note>**Note** — Only applicable when `jsx` is `react`.</Note>
|
|
44
|
+
|
|
45
|
+
The function name used to represent JSX constructs. Default value is `"createElement"`. This is useful for libraries like [Preact](https://preactjs.com/) that use a different function name (`"h"`).
|
|
46
|
+
|
|
47
|
+
| Compiler options | Transpiled output |
|
|
48
|
+
| --------------------------------------------------------------------- | ------------------------------------------------------------------------------ |
|
|
49
|
+
| `json<br/>{<br/> "jsx": "react",<br/> "jsxFactory": "h"<br/>}<br/>` | `tsx<br/>import { h } from "react";<br/>h("Box", { width: 5 }, "Hello");<br/>` |
|
|
50
|
+
|
|
51
|
+
### [`jsxFragmentFactory`](https://www.typescriptlang.org/tsconfig#jsxFragmentFactory)
|
|
52
|
+
|
|
53
|
+
<Note>**Note** — Only applicable when `jsx` is `react`.</Note>
|
|
54
|
+
|
|
55
|
+
The function name used to represent [JSX fragments](https://react.dev/reference/react/Fragment) such as `<>Hello</>`; only applicable when `jsx` is `react`. Default value is `"Fragment"`.
|
|
56
|
+
|
|
57
|
+
| Compiler options | Transpiled output |
|
|
58
|
+
| ------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
59
|
+
| `json<br/>{<br/> "jsx": "react",<br/> "jsxFactory": "myjsx",<br/> "jsxFragmentFactory": "MyFragment"<br/>}<br/>` | `tsx<br/>// input<br/><>Hello</>;<br/><br/>// output<br/>import { myjsx, MyFragment } from "react";<br/>myjsx(MyFragment, null, "Hello");<br/>` |
|
|
60
|
+
|
|
61
|
+
### [`jsxImportSource`](https://www.typescriptlang.org/tsconfig#jsxImportSource)
|
|
62
|
+
|
|
63
|
+
<Note>**Note** — Only applicable when `jsx` is `react-jsx` or `react-jsxdev`.</Note>
|
|
64
|
+
|
|
65
|
+
The module from which the component factory function (`createElement`, `jsx`, `jsxDEV`, etc) will be imported. Default value is `"react"`. This will typically be necessary when using a component library like Preact.
|
|
66
|
+
|
|
67
|
+
| Compiler options | Transpiled output |
|
|
68
|
+
| ------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
69
|
+
| `jsonc<br/>{<br/> "jsx": "react",<br/> // jsxImportSource is not defined<br/> // default to "react"<br/>}<br/>` | `tsx<br/>import { jsx } from "react/jsx-runtime";<br/>jsx("Box", { width: 5, children: "Hello" });<br/>` |
|
|
70
|
+
| `jsonc<br/>{<br/> "jsx": "react-jsx",<br/> "jsxImportSource": "preact",<br/>}<br/>` | `tsx<br/>import { jsx } from "preact/jsx-runtime";<br/>jsx("Box", { width: 5, children: "Hello" });<br/>` |
|
|
71
|
+
| `jsonc<br/>{<br/> "jsx": "react-jsxdev",<br/> "jsxImportSource": "preact",<br/>}<br/>` | `tsx<br/>// /jsx-runtime is automatically appended<br/>import { jsxDEV } from "preact/jsx-dev-runtime";<br/>jsxDEV(<br/> "Box",<br/> { width: 5, children: "Hello" },<br/> undefined,<br/> false,<br/> undefined,<br/> this,<br/>);<br/>` |
|
|
72
|
+
|
|
73
|
+
### JSX pragma
|
|
74
|
+
|
|
75
|
+
All of these values can be set on a per-file basis using _pragmas_. A pragma is a special comment that sets a compiler option in a particular file.
|
|
76
|
+
|
|
77
|
+
| Pragma | Equivalent config |
|
|
78
|
+
| ---------------------------------------- | ------------------------------------------------------------------ |
|
|
79
|
+
| `ts<br/>// @jsx h<br/>` | `jsonc<br/>{<br/> "jsxFactory": "h",<br/>}<br/>` |
|
|
80
|
+
| `ts<br/>// @jsxFrag MyFragment<br/>` | `jsonc<br/>{<br/> "jsxFragmentFactory": "MyFragment",<br/>}<br/>` |
|
|
81
|
+
| `ts<br/>// @jsxImportSource preact<br/>` | `jsonc<br/>{<br/> "jsxImportSource": "preact",<br/>}<br/>` |
|
|
82
|
+
|
|
83
|
+
## Logging
|
|
84
|
+
|
|
85
|
+
Bun implements special logging for JSX to make debugging easier. Given the following file:
|
|
86
|
+
|
|
87
|
+
```tsx index.tsx icon="/icons/typescript.svg"
|
|
88
|
+
import { Stack, UserCard } from "./components";
|
|
89
|
+
|
|
90
|
+
console.log(
|
|
91
|
+
<Stack>
|
|
92
|
+
<UserCard name="Dom" bio="Street racer and Corona lover" />
|
|
93
|
+
<UserCard name="Jakob" bio="Super spy and Dom's secret brother" />
|
|
94
|
+
</Stack>,
|
|
95
|
+
);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Bun will pretty-print the component tree when logged:
|
|
99
|
+
|
|
100
|
+
<Frame></Frame>
|
|
101
|
+
|
|
102
|
+
## Prop punning
|
|
103
|
+
|
|
104
|
+
The Bun runtime also supports "prop punning" for JSX. This is a shorthand syntax useful for assigning a variable to a prop with the same name.
|
|
105
|
+
|
|
106
|
+
```tsx react.tsx icon="/icons/typescript.svg"
|
|
107
|
+
function Div(props: {className: string;}) {
|
|
108
|
+
const {className} = props;
|
|
109
|
+
|
|
110
|
+
// without punning
|
|
111
|
+
return <div className={className} />;
|
|
112
|
+
// with punning
|
|
113
|
+
return <div {className} />;
|
|
114
|
+
}
|
|
115
|
+
```
|