How to Convert an Object to a String with Bun.inspect
In Bun, calling Bun.inspect(value) returns a string in the same form that console.log prints. Objects that become [object Object] when put in a template string, Map values that JSON.stringify turns into an empty {}, and circular references that make JSON.stringify throw can all be turned into readable strings.
This is useful when writing logs to a file or displaying an object's contents for debugging. In addition to the basic usage, this article shows the four options (depth, sorted, compact, colors), Bun.inspect.custom for changing how your own class is displayed, and the output of Bun.inspect.table, which formats data as a table.
The tests were run on Ubuntu 24.04 (x86_64) with Bun 1.4.4. Each script was run once beforehand, and the results shown are from the second and later runs. The official documentation and type definitions were checked on GitHub at the tag used for testing (bun-v1.4.4). I have not tested macOS, Windows, or any Bun version other than 1.4.4.
- 1. Bun.inspect() returns the same string that console.log prints
- 2. Values that JSON.stringify drops can still be displayed
- 3. Use depth to control how deep the output goes
- 4. Use sorted and compact to change key order and line breaks
- 5. Use colors: true to add color for the terminal
- 6. Use Bun.inspect.custom to control how your own class is displayed
- 7. The output differs from node:util's inspect
- 8. Use Bun.inspect.table to turn an array into a table string
Bun.inspect() returns the same string that console.log prints
The official utilities documentation page states that Bun.inspect() turns a value into a string exactly as console.log displays it. I compared an object containing a date and an array against a template string and against the result of JSON.stringify.
// basic.ts
const user = { id: 1, name: "Hanako", tags: ["admin", "dev"], createdAt: new Date("2026-11-24T00:00:00Z") };
console.log(`テンプレート文字列: ${user}`);
console.log("JSON.stringify:", JSON.stringify(user));
const s = Bun.inspect(user);
console.log(`Bun.inspect の型: ${typeof s} / 長さ: ${s.length}`);
console.log(s);
$ bun basic.ts
テンプレート文字列: [object Object]
JSON.stringify: {"id":1,"name":"Hanako","tags":["admin","dev"],"createdAt":"2026-11-24T00:00:00.000Z"}
Bun.inspect の型: string / 長さ: 97
{
id: 1,
name: "Hanako",
tags: [ "admin", "dev" ],
createdAt: 2026-11-24T00:00:00.000Z,
}
Bun.inspect(user) is a 97-character string with line breaks and indentation. Keys are not wrapped in ", and the last property also ends with a comma. This is how Bun formats output.
To confirm that it matches console.log(user) exactly, I ran a separate process that wrote console.log(user) and Bun.inspect(user) to standard output and compared them. The result was true (98 characters including the line break), so the two strings were identical.
Values that JSON.stringify drops can still be displayed
JSON.stringify only outputs values that can be expressed as JSON, so some values become unreadable when logged. I passed several values to both and compared the results.
| Value | Bun.inspect |
JSON.stringify |
|---|---|---|
new Uint8Array([1, 2, 3]) |
Uint8Array(3) [ 1, 2, 3 ] |
{"0":1,"1":2,"2":3} |
new Map([["a", 1], ["b", 2]]) |
Map(2) { "a": 1, "b": 2, } (actually multi-line) |
{} |
new Set([1, 2, 3]) |
Set(3) { 1, 2, 3, } (actually multi-line) |
{} |
{ a: undefined, b: null } |
Shows both a: undefined and b: null |
{"b":null} |
Symbol("id") |
Symbol(id) |
undefined |
function hello() {} |
[Function: hello] |
undefined |
Promise.resolve(42) |
Promise { <resolved> } |
{} |
Instance of the Point class |
Point { x: 1, y: 2, } (actually multi-line) |
{"x":1,"y":2} |
JSON.stringify turns Map and Set into {}, but Bun.inspect shows their size and contents. Class instances are shown with the class name, as in Point. For Promise, only <resolved> was shown, and the inner value 42 did not appear.
Circular references do not throw
I passed an object that references itself.
const loop: any = { name: "loop" };
loop.self = loop;
console.log(Bun.inspect(loop));
try { JSON.stringify(loop); } catch (e) { console.log("JSON.stringify:", (e as Error).name, (e as Error).message); }
{
name: "loop",
self: [Circular],
}
JSON.stringify: TypeError JSON.stringify cannot serialize cyclic structures.
JSON.stringify throws a TypeError, but Bun.inspect displays the point where the reference returns as [Circular] and still produces a complete string.
Use depth to control how deep the output goes
According to the Bun type definitions (bun.d.ts), the second argument of Bun.inspect accepts four options: colors, depth, sorted, and compact.
First, depth. I converted an object nested 12 levels deep to a string without any options. The output was long, so I extracted only the lines containing level or the abbreviated lines and joined them with / on one line.
// defaultdepth.ts
let o: any = { level: 12 };
for (let i = 11; i >= 1; i--) o = { level: i, child: o };
const out = Bun.inspect(o);
const lines = out.split("\n");
console.log(lines.filter(l => l.includes("level") || l.includes("...")).map(l => l.trim()).join(" / "));
$ bun defaultdepth.ts
level: 1, / level: 2, / level: 3, / level: 4, / level: 5, / level: 6, / level: 7, / level: 8, / level: 9, / child: [Object ...],
Without options, output is shown down to the ninth level from the outermost object (level: 9), and everything below that is abbreviated as [Object ...]. Passing depth: 1 abbreviates it earlier.
const deep = { a: { b: { c: { d: { e: "深い" } } } } };
console.log(Bun.inspect(deep, { depth: 1 }));
{
a: {
b: [Object ...],
},
}
Passing depth: 10 or depth: Infinity to the same object displayed everything down to the innermost e property. If you want to keep an entire deeply nested configuration object in a log, increase depth.
Long arrays are cut off after 100 items
When I passed an array of 150 numbers from 0 to 149, the output ended after the 100th item, 99, followed by ... 50 more items. Below are the last 3 lines of the 8-line output.
83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
... 50 more items
]
On the other hand, for an object whose value is a 20,000-character string, the output was 20,012 characters and the string was included in full without being truncated.
Use sorted and compact to change key order and line breaks
I tried sorted and compact on an object whose keys were in no particular order.
const obj = { zeta: 1, alpha: 2, mid: [1, 2, 3], nested: { y: true, x: false } };
console.log(Bun.inspect(obj, { sorted: true }));
console.log(Bun.inspect(obj, { compact: true }));
{
alpha: 2,
mid: [
1,
2,
3
],
nested: {
x: false,
y: true,
},
zeta: 1,
}
{ zeta: 1, alpha: 2, mid: [ 1, 2, 3 ], nested: { y: true, x: false } }
With sorted: true, keys were sorted by name, including those inside nested objects. However, in this version, an array that is shown on one line without options ([ 1, 2, 3 ]) was split into one element per line. With compact: true, the whole output is on one line. Passing compact: false gave the same output as the default.
For logs where each entry is written on one line, compact: true is useful. In the following example, error information was put on one line and written to a log file.
const event = { level: "error", at: new Date("2026-11-24T09:00:00Z"), detail: { code: 500, path: "/api/items" } };
const line = `[${event.at.toISOString()}] ${Bun.inspect(event.detail, { compact: true })}\n`;
await Bun.write("app.log", line);
$ cat app.log
[2026-11-24T09:00:00.000Z] { code: 500, path: "/api/items" }
Use colors: true to add color for the terminal
Passing colors: true puts terminal control sequences (ANSI escape sequences) into the string to add color. Numbers, true, and null got yellow (ANSI 33), and strings got green (32). The colors you actually see depend on your terminal's color scheme. The following image shows the output drawn in a terminal-like style, using the color codes from the output.

Without the option, no color was added, even when writing to a terminal. When I called Bun.inspect({ n: 1 }) on a pseudo-terminal, the returned string contained no control characters (ESC), even though process.stdout.isTTY was true.
If you write a colored string to a file, the control characters remain as they are. Here is { n: 1, s: "a" } after converting the control characters to a visible form with JSON.stringify:
"{\n \u001b[0mn\u001b[2m:\u001b[0m \u001b[0m\u001b[33m1\u001b[0m\u001b[0m\u001b[2m,\u001b[0m\n \u001b[0ms\u001b[2m:\u001b[0m \u001b[0m\u001b[32m\u001b[0m\u001b[32m\"a\"\u001b[0m\u001b[0m\u001b[0m\u001b[2m,\u001b[0m\n}"
For strings you keep in log files, leaving out colors makes them easier to read.
Use Bun.inspect.custom to control how your own class is displayed
If you define a method named [Bun.inspect.custom]() in a class, you can decide the string that Bun.inspect and console.log display for its instances (official documentation). The example below is a class holding a password, which is hidden only when it appears in logs.
The actual custom.ts also contains a line that prints the arguments received by the method, and a comparison with util.inspect.custom, which is covered later. The following code and output leave out those lines.
// custom.ts (partial)
class User {
constructor(public name: string, private password: string) {}
[Bun.inspect.custom](depth: number, options: unknown) {
return `User(${this.name}, password: ***)`;
}
}
const u = new User("hanako", "s3cret");
console.log(Bun.inspect(u));
console.log(u);
console.log(Bun.inspect({ owner: u }));
console.log("JSON.stringify:", JSON.stringify(u));
$ bun custom.ts
User(hanako, password: ***)
User(hanako, password: ***)
{
owner: User(hanako, password: ***),
}
JSON.stringify: {"name":"hanako","password":"s3cret"}
The string returned by the method was used whether it was called through Bun.inspect(u) or console.log(u), and also when the instance was nested inside another object. JSON.stringify does not use this method, so the password appears as it is. When writing with JSON.stringify, you need a separate measure in addition to this method.
The method received a number as its first argument and an object as its second. When it was called from Bun.inspect(u), the first argument was 8.
The official documentation says that Bun.inspect.custom is the same as Node.js's util.inspect.custom. When I compared them, both Bun.inspect.custom === util.inspect.custom and Bun.inspect.custom === Symbol.for("nodejs.util.inspect.custom") were true.
The output differs from node:util's inspect
Bun also lets you use Node.js's util.inspect via import util from "node:util", but its output is not the same as Bun.inspect.
// nodeutil.ts
import util from "node:util";
const obj = { id: 1, list: [1, 2], nested: { a: { b: { c: {} } } } };
console.log("Bun.inspect:\n" + Bun.inspect(obj));
console.log("util.inspect:\n" + util.inspect(obj));
console.log("同じ文字列か:", Bun.inspect(obj) === util.inspect(obj));
Bun.inspect:
{
id: 1,
list: [ 1, 2 ],
nested: {
a: {
b: {
c: {},
},
},
},
}
util.inspect:
{ id: 1, list: [ 1, 2 ], nested: { a: { b: [Object] } } }
同じ文字列か: false
util.inspect put the short object on one line, and it abbreviated everything from b inside a inside nested as [Object]. If you want output that matches Node.js, use util.inspect. If you want output that matches Bun's console.log, use Bun.inspect.
Use Bun.inspect.table to turn an array into a table string
Bun.inspect.table() returns the same table that console.table prints, as a string instead of displaying it (official documentation). If you pass an array of property names as the second argument, only those columns are shown.
const rows = [
{ name: "apple", price: 120, stock: 5 },
{ name: "banana", price: 80, stock: 0 },
{ name: "りんご", price: 150, stock: 12 },
];
console.log(Bun.inspect.table(rows));
console.log(Bun.inspect.table(rows, ["name", "stock"]));
┌───┬────────┬───────┬───────┐
│ │ name │ price │ stock │
├───┼────────┼───────┼───────┤
│ 0 │ apple │ 120 │ 5 │
│ 1 │ banana │ 80 │ 0 │
│ 2 │ りんご │ 150 │ 12 │
└───┴────────┴───────┴───────┘
┌───┬────────┬───────┐
│ │ name │ stock │
├───┼────────┼───────┤
│ 0 │ apple │ 5 │
│ 1 │ banana │ 0 │
│ 2 │ りんご │ 12 │
└───┴────────┴───────┘
The return value is a string. Full-width characters, such as the Japanese word for apple in the table, are counted as two columns wide, so the box-drawing borders stayed aligned.
If you pass an object instead of an array, its keys become the row labels. When rows have different properties, the missing cells are left blank.
console.log(Bun.inspect.table({ a: { x: 1 }, b: { x: 2, y: 3 } }));
┌───┬───┬───┐
│ │ x │ y │
├───┼───┼───┤
│ a │ 1 │ │
│ b │ 2 │ 3 │
└───┴───┴───┘
The only option for Bun.inspect.table is colors. The type definition also gives it as { colors?: boolean } (bun.d.ts).





