Skip to main content

Bun.version

A string containing the version of the bun CLI that is currently running.
terminal

Bun.revision

The git commit of Bun that was compiled to create the current bun CLI.
terminal

Bun.env

An alias for process.env.

Bun.main

An absolute path to the entrypoint of the current program (the file that was executed with bun run).
script.ts
This is particular useful for determining whether a script is being directly executed, as opposed to being imported by another script.
This is analogous to the require.main = module trick in Node.js.

Bun.sleep()

Bun.sleep(ms: number) Returns a Promise that resolves after the given number of milliseconds.
Alternatively, pass a Date object to receive a Promise that resolves at that point in time.

Bun.sleepSync()

Bun.sleepSync(ms: number) A blocking synchronous version of Bun.sleep.

Bun.which()

Bun.which(bin: string) Returns the path to an executable, similar to typing which in your terminal.
By default Bun looks at the current PATH environment variable to determine the path. To configure PATH:
Pass a cwd option to resolve for executable from within a specific directory.
You can think of this as a builtin alternative to the which npm package.

Bun.randomUUIDv7()

Bun.randomUUIDv7() returns a UUID v7, which is monotonic and suitable for sorting and databases.
A UUID v7 is a 128-bit value that encodes the current timestamp, a random value, and a counter. The timestamp is encoded using the lowest 48 bits, and the random value and counter are encoded using the remaining bits. The timestamp parameter defaults to the current time in milliseconds. When the timestamp changes, the counter is reset to a pseudo-random integer wrapped to 4096. This counter is atomic and threadsafe, meaning that using Bun.randomUUIDv7() in many Workers within the same process running at the same timestamp will not have colliding counter values. The final 8 bytes of the UUID are a cryptographically secure random value. It uses the same random number generator used by crypto.randomUUID() (which comes from BoringSSL, which in turn comes from the platform-specific system random number generator usually provided by the underlying hardware).
You can optionally set encoding to "buffer" to get a 16-byte buffer instead of a string. This can sometimes avoid string conversion overhead.
buffer.ts
base64 and base64url encodings are also supported when you want a slightly shorter string.
base64.ts

Bun.peek()

Bun.peek(prom: Promise) Reads a promise’s result without await or .then, but only if the promise has already fulfilled or rejected.
This is important when attempting to reduce the number of extraneous microticks in performance-sensitive code. It’s an advanced API — review the examples below before using it in production.
The peek.status function lets you read the status of a promise without resolving it.

Bun.openInEditor()

Opens a file in your default editor. Bun auto-detects your editor via the $VISUAL or $EDITOR environment variables.
You can override this via the debug.editor setting in your bunfig.toml.
bunfig.toml
Or specify an editor with the editor param. You can also specify a line and column number.

Bun.deepEquals()

Recursively checks if two objects are equivalent. expect().toEqual() in bun:test uses this internally.
Pass a third boolean parameter to enable “strict” mode. expect().toStrictEqual() in the test runner uses this.
In strict mode, the following are considered unequal:

Bun.escapeHTML()

Bun.escapeHTML(value: string | object | number | boolean): string Escapes the following characters from an input string:
  • " becomes "
  • & becomes &
  • ' becomes '
  • < becomes &lt;
  • > becomes &gt;
This function is optimized for large input. On an M1X, it processes 480 MB/s - 20 GB/s, depending on how much data is being escaped and whether there is non-ascii text. Non-string types will be converted to a string before escaping.

Bun.stringWidth()

~6,756x faster string-width alternative
Get the column count of a string as it would be displayed in a terminal. Supports ANSI escape codes, emoji, and wide characters. Example usage:
This is useful for:
  • Aligning text in a terminal
  • Quickly checking if a string contains ANSI escape codes
  • Measuring the width of a string in a terminal
This API is designed to match the popular “string-width” package, so that existing code can be ported to Bun and vice versa. In this benchmark, Bun.stringWidth is a ~6,756x faster than the string-width npm package for input larger than about 500 characters. Big thanks to sindresorhus for their work on string-width!
To make Bun.stringWidth fast, we’ve implemented it in native code using optimized SIMD instructions, accounting for Latin1, UTF-16, and UTF-8 encodings. It passes string-width’s tests.
As a reminder, 1 nanosecond (ns) is 1 billionth of a second. Here’s a reference for converting between units:
terminal
terminal
TypeScript definition:

Bun.fileURLToPath()

Converts a file:// URL to an absolute path.

Bun.pathToFileURL()

Converts an absolute path to a file:// URL.

Bun.gzipSync()

Compresses a Uint8Array using zlib’s GZIP algorithm.
Optionally, pass a parameters object as the second argument:

Bun.gunzipSync()

Decompresses a Uint8Array using zlib’s GUNZIP algorithm.

Bun.deflateSync()

Compresses a Uint8Array using zlib’s DEFLATE algorithm.
The second argument supports the same set of configuration options as Bun.gzipSync.

Bun.inflateSync()

Decompresses a Uint8Array using zlib’s INFLATE algorithm.

Bun.zstdCompress() / Bun.zstdCompressSync()

Compresses a Uint8Array using the Zstandard algorithm.

Bun.zstdDecompress() / Bun.zstdDecompressSync()

Decompresses a Uint8Array using the Zstandard algorithm.

Bun.inspect()

Serializes an object to a string exactly as it would be printed by console.log.

Bun.inspect.custom

This is the symbol that Bun uses to implement Bun.inspect. You can override this to customize how your objects are printed. It is identical to util.inspect.custom in Node.js.

Bun.inspect.table(tabularData, properties, options)

Format tabular data into a string. Like console.table, except it returns a string rather than printing to the console.
Additionally, you can pass an array of property names to display only a subset of properties.
You can also conditionally enable ANSI colors by passing { colors: true }.

Bun.nanoseconds()

Returns the number of nanoseconds since the current bun process started, as a number. Useful for high-precision timing and benchmarking.

Bun.readableStreamTo*()

Bun implements a set of convenience functions for asynchronously consuming the body of a ReadableStream and converting it to various binary formats.

Bun.resolveSync()

Resolves a file path or module specifier using Bun’s internal module resolution algorithm. The first argument is the path to resolve, and the second argument is the “root”. If no match is found, an Error is thrown.
To resolve relative to the current working directory, pass process.cwd() or "." as the root.
To resolve relative to the directory containing the current file, pass import.meta.dir.

Bun.stripANSI()

~6-57x faster strip-ansi alternative
Bun.stripANSI(text: string): string Strip ANSI escape codes from a string. This is useful for removing colors and formatting from terminal output.
Bun.stripANSI is significantly faster than the popular strip-ansi npm package:
terminal
terminal

Bun.wrapAnsi()

Drop-in replacement for wrap-ansi npm package
Bun.wrapAnsi(input: string, columns: number, options?: WrapAnsiOptions): string Wrap text to a specified column width while preserving ANSI escape codes, hyperlinks, and handling Unicode/emoji width correctly. This is a native, high-performance alternative to the popular wrap-ansi npm package.

Options

TypeScript definition:

serialize & deserialize in bun:jsc

To save a JavaScript value into an ArrayBuffer & back, use serialize and deserialize from the "bun:jsc" module.
Internally, structuredClone and postMessage serialize and deserialize the same way. This exposes the underlying HTML Structured Clone Algorithm to JavaScript as an ArrayBuffer.

estimateShallowMemoryUsageOf in bun:jsc

The estimateShallowMemoryUsageOf function returns a best-effort estimate of the memory usage of an object in bytes, excluding the memory usage of properties or other objects it references. For accurate per-object memory usage, use Bun.generateHeapSnapshot.