- Parse TOML strings with
Bun.TOML.parse import&requireTOML files as modules at runtime (including hot reloading & watch mode support)import&requireTOML files in frontend apps with Bun’s bundler
Runtime API
Bun.TOML.parse()
Parse a TOML string into a JavaScript object.
Supported TOML Features
Bun’s TOML parser implements the full TOML v1.1.0 specification and passes the complete official toml-test conformance suite.- Strings: basic (
"...") and literal ('...'), including multi-line, with all escapes (\uHHHH,\UHHHHHHHH, and TOML 1.1’s\xHHand\e) - Integers: decimal, hex (
0x), octal (0o), and binary (0b). Integers outside ±(2^53 - 1) throw, because a JavaScript number cannot represent them losslessly - Floats: including
infandnan - Booleans:
trueandfalse - Date/times: returned as Temporal objects — offset date-time as
Temporal.Instant, local date-time asTemporal.PlainDateTime, local date asTemporal.PlainDate, and local time asTemporal.PlainTime - Arrays: including mixed types and nested arrays
- Tables: standard (
[table]) and inline ({ key = "value" }), including TOML 1.1 multi-line inline tables - Array of tables:
[[array]] - Dotted keys:
a.b.c = "value" - Comments: using
#
Date/times
Each of TOML’s four date/time types maps 1:1 onto a Temporal type. Temporal carries nanosecond precision; as the TOML spec permits, fractional seconds beyond nine digits are truncated:Error Handling
Bun.TOML.parse() throws a SyntaxError if the TOML is invalid:
Bun.TOML.stringify()
Serialize a JavaScript object to a TOML document. Scalar keys come first,
followed by [table] and [[array-of-tables]] sections:
Temporal.Instant, Temporal.PlainDateTime, Temporal.PlainDate, and
Temporal.PlainTime values become the corresponding TOML date/time
literals, so stringify(parse(doc)) round-trips date/time types.
Temporal.ZonedDateTime becomes an offset date-time and Date becomes
an offset date-time in UTC. TOML has no syntax for time-zone or calendar
annotations, so those are dropped (the ISO fields are written), and its
years are four digits, so date values outside 0000–9999 and invalid
Dates throw. Because TOML cannot represent them, null values,
BigInt, circular structures, Temporal.PlainYearMonth,
Temporal.PlainMonthDay, and Temporal.Duration also throw; undefined,
function, and symbol properties are skipped (inside arrays they throw,
since TOML arrays cannot have holes), and passing one of those as the
top-level value returns undefined, as JSON.stringify does.
Module Import
ES Modules
Import TOML files directly as ES modules. Bun parses the TOML and exposes it as both default and named exports:config.toml
Default Import
app.ts
Named Imports
You can destructure top-level TOML tables as named imports:app.ts
app.ts
Import Attributes
Use an import attribute to load any file as TOML:app.ts
CommonJS
You can alsorequire TOML files in CommonJS:
app.ts
Hot Reloading with TOML
When you run your application withbun --hot, Bun detects changes to TOML files and reloads them without restarting:
config.toml
server.ts
terminal
Bundler Integration
When you bundle with Bun, the bundler parses imported TOML at build time and includes it as a JavaScript module:terminal
- Zero runtime TOML parsing overhead in production
- Smaller bundle sizes
- Tree shaking of unused properties (named imports)