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
Use this to determine whether a script is being executed directly, 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 the executable from within a specific directory.
This is a built-in 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 clock moves forward, the counter is reseeded to a new pseudo-random integer (the high bit of the 12-bit counter is kept clear so at least 2048 values remain before rollover). If the clock has not advanced past the last emitted timestamp, Bun reuses the last emitted timestamp and increments the counter. If that counter rolls over, Bun bumps the emitted timestamp forward instead of wrapping the counter, so the returned UUIDs stay strictly increasing (RFC 9562 §6.2). The counter is atomic and threadsafe, so calls to Bun.randomUUIDv7() from many Workers in the same process at the same timestamp don’t produce colliding counter values. When you pass an explicit timestamp, Bun encodes that value verbatim and tracks a separate counter for it, so explicit-timestamp calls do not observe or alter the monotonic state used by the default path. Repeated calls with the same explicit timestamp increment that separate counter (and bump the emitted timestamp on rollover) so they stay sortable; a call with a different explicit timestamp reseeds it. 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).
Pass "buffer" as the encoding to get a 16-byte buffer instead of a string. This can 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.
Use it to avoid extraneous microticks in performance-sensitive code. It’s an advanced API; review the following examples before using it in production.
peek.status reads the status of a promise without resolving it.

Bun.openInEditor()

Opens a file in your default editor. Bun auto-detects your editor from the $VISUAL or $EDITOR environment variables.
You can override this with 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 are 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:
Use it to align text in a terminal or to check whether a string contains ANSI escape codes. The API matches the “string-width” npm package, so existing code can be ported to Bun and vice versa. In this benchmark, Bun.stringWidth is ~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.
Bun.stringWidth is implemented in native code with SIMD instructions and accounts for Latin1, UTF-16, and UTF-8 encodings. It passes string-width’s tests.
1 nanosecond (ns) is 1 billionth of a second. 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

The symbol Bun uses to implement Bun.inspect. Override it 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.
Pass an array of property names to display only those properties.
Pass { colors: true } to enable ANSI colors.

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, it throws an Error.
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. Use it to remove colors and formatting from terminal output.
Bun.stripANSI is faster than the 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. It preserves ANSI escape codes and hyperlinks and handles Unicode/emoji width correctly. This is a native alternative to the wrap-ansi npm package.

Options

TypeScript definition:

serialize & deserialize in bun:jsc

To save a JavaScript value into a SharedArrayBuffer & 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 a SharedArrayBuffer.

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.