How to Import TOML and JSONC Files in Bun
In Bun, you can import TOML and JSONC (JSON with comments) files directly, without installing any package. The usual pattern is to keep app settings in config.toml or settings.jsonc and read them from TypeScript code as JavaScript objects. Here, I actually imported the files to see what shape the values take, what happens when a file can't be read, and where things go wrong with the build and type checking.
The test environment was Ubuntu 24.04 (x86_64, 4 CPUs), Bun 1.4.4, TypeScript 7.0.2, and @types/bun 1.4.4. For comparison, I also used Node.js 22.23.5. I replaced the working directory path in error output with /path/to/app, and I added file names to the first-line comments in the code examples. I did not test Windows or macOS, reloading with bun --hot, loading with require(), browser builds, or type checking with TypeScript 7.1 or later. For the official documentation, I checked the source files at the tested release (tag bun-v1.4.4) on GitHub, and the links point there.
- 1. TOML can be imported both as a default and by name
- 2. JSONC reads files after removing comments and trailing commas
- 3. Files that can't be read fail at import time
- 4. If the file is chosen at runtime, you can read it with Bun.TOML.parse
- 5. With bun build, the contents are written into the bundle
- 6. In TypeScript, named imports need type declarations
TOML can be imported both as a default and by name
Bun loads files with the .toml extension as TOML (the toml description in the official loaders list). Top-level tables can also be pulled out with named imports (the official description of TOML imports). I tested with the following config.toml.
# App settings
title = "neko-shop"
maxItems = 50
[server]
host = "localhost"
port = 3000
[database]
host = "db.example.com"
port = 5432
tags = ["main", "read"]
[[plans]]
name = "free"
price = 0
[[plans]]
name = "pro"
price = 980
[schedule]
releasedAt = 2026-11-21T09:00:00+09:00
openDate = 2026-12-01
opens = 07:30:00
// toml-app.ts: import TOML as both default and named
import config, { server, plans } from "./config.toml";
console.log(config.title, config.maxItems);
console.log(server.host, server.port);
console.log(plans.map((p: { name: string; price: number }) => `${p.name}:${p.price}`).join(", "));
console.log(config.database.tags);
$ bun toml-app.ts
neko-shop 50
localhost 3000
free:0, pro:980
[ "main", "read" ]
The port value 3000 came through as a number, and tags stayed an array. The table array written twice with [[plans]] became an array containing two objects.
Dates and times become Temporal values, not Date
In TOML, dates and times can be written without quotes. Bun turns these into Temporal objects, not JavaScript Date objects (the official description of TOML dates and times). I printed the three value types in [schedule].
// toml-dates.ts: shows what values TOML dates and times become
import { schedule } from "./config.toml";
for (const [key, value] of Object.entries(schedule)) {
console.log(key, (value as object).constructor.name, String(value));
}
$ bun toml-dates.ts
releasedAt Instant 2026-11-21T00:00:00Z
openDate PlainDate 2026-12-01
opens PlainTime 07:30:00
releasedAt, which was written with the +09:00 offset, became a Temporal.Instant. When converted to a string, it showed the UTC value 2026-11-21T00:00:00Z. The original +09:00 offset was not kept. A date-only value becomes a Temporal.PlainDate, and a time-only value becomes a Temporal.PlainTime.
JSONC reads files after removing comments and trailing commas
Files with the .jsonc extension are loaded after removing // and /* */ comments and commas after the last element (the jsonc description in the official loaders list).
{
// Display settings
"theme": "dark",
"fontSize": 14,
/* Experimental features */
"experimental": {
"newEditor": true,
},
"plugins": ["git", "lint",],
}
// jsonc-app.ts: import JSONC as both default and named
import settings, { experimental } from "./settings.jsonc";
console.log(settings);
console.log(experimental.newEditor);
$ bun jsonc-app.ts
{
theme: "dark",
fontSize: 14,
experimental: {
newEditor: true,
},
plugins: [ "git", "lint" ],
}
true
With JSONC too, I could import a top-level key (experimental) by name. When I imported { "a": 1, "a": 2 }, which has the same key twice, there was no error, and only the later a: 2 remained.
For files without a .jsonc or .toml extension, specify the format with an import attribute
I copied the same content as settings.jsonc into a file named settings-commented.json and imported it without an import attribute.
// json-comment.ts: import a file with comments while keeping the .json extension
import settings from "./settings-commented.json";
console.log(settings.theme);
When I ran it, the file was read as JSON, and it failed on the / of the comment. The error did not show which file or line caused it.
$ bun json-comment.ts
SyntaxError: JSON Parse error: Unrecognized token '/'
Bun v1.4.4 (Linux x64)
If you can't rename the file, specify how to read it with the type import attribute (the official description of specifying a loader with an import attribute).
// json-attr.ts: keep the .json extension and read it as jsonc using an import attribute
import settings from "./settings-commented.json" with { type: "jsonc" };
console.log(settings.theme, settings.plugins);
$ bun json-attr.ts
dark [ "git", "lint" ]
Also, files named tsconfig.json, jsconfig.json, package.json, and bun.lock are read as JSONC automatically (the official note on jsonc). A tsconfig.json containing comments and a trailing comma imported without an attribute was read correctly.
The same approach works for files with other extensions, such as .conf, which can be read as TOML too. When I imported app.conf, which contains name = "conf-file" and debug = true, with import conf from "./app.conf" with { type: "toml" };, I got { name: "conf-file", debug: true }.
Files that can't be read fail at import time
When I imported a TOML file with a syntax error, the import stopped right away and showed the line and column.
title = "neko-shop"
port = = 3000
// broken-import.ts
import broken from "./broken.toml";
console.log(broken);
$ bun broken-import.ts
2 | port = = 3000
^
error: Expected a value but found '='
at /path/to/app/broken.toml:2:8
Bun v1.4.4 (Linux x64)
Large integers that JavaScript numbers can't represent exactly also cause errors (the official description of values allowed in TOML). With a file containing id = 9007199254740993, the import stopped like this:
$ bun bigint-import.ts
1 | id = 9007199254740993
^
error: Integer cannot be losslessly represented as a JavaScript number; it must be within +/-(2^53 - 1)
at /path/to/app/bigint.toml:1:6
Bun v1.4.4 (Linux x64)
When the same value was written in quotes as id = "9007199254740993", it was read as a string. Passing it to BigInt(data.id) gave 9007199254740993n, with no loss of digits.
If the file is chosen at runtime, you can read it with Bun.TOML.parse
A static import statement names the file in the code. If you need to choose the file at runtime, for example switching between config.dev.toml and config.prod.toml based on the value of APP_ENV, you can read the file contents as a string and pass them to Bun.TOML.parse() or Bun.JSONC.parse().
// runtime-parse.ts: decide which file to read at runtime and read it with Bun.TOML.parse and Bun.JSONC.parse
type ServerConfig = { server: { host: string; port: number } };
type Settings = { theme: string };
const env = process.env.APP_ENV ?? "dev";
const toml = Bun.TOML.parse(await Bun.file(`./config.${env}.toml`).text()) as ServerConfig;
const jsonc = Bun.JSONC.parse(await Bun.file("./settings.jsonc").text()) as Settings;
console.log(env, toml.server.host, toml.server.port, jsonc.theme);
export {};
$ bun runtime-parse.ts
dev localhost 3000 dark
$ env APP_ENV=prod bun runtime-parse.ts
prod 0.0.0.0 8080 dark
In Bun's type definitions, the return type of Bun.TOML.parse() is object, and the return type of Bun.JSONC.parse() is unknown (TOML in Bun's type definitions and JSONC). That is why the code above uses as to assign types. The type definition comments also say that passing Bun.file() directly to Bun.TOML.parse() causes an error, so the code converts the file to a string first with await file.text().
With bun build, the contents are written into the bundle
When you bundle with bun build, TOML contents are parsed and included in the bundle as JavaScript objects (the official description of TOML handling in the bundler). Here is the part for config.toml from building toml-app.ts:
// config.toml
var server = {
host: "localhost",
port: 3000
};
var plans = [
// (contents of 2 items omitted)
];
var config_default = {
title: "neko-shop",
maxItems: 50,
server,
database: {
host: "db.example.com",
port: 5432,
tags: [
"main",
"read"
]
},
plans,
schedule: {
releasedAt: /* @__PURE__ */ Temporal.Instant.from("2026-11-21T09:00:00+09:00"),
openDate: /* @__PURE__ */ Temporal.PlainDate.from("2026-12-01"),
opens: /* @__PURE__ */ Temporal.PlainTime.from("07:30:00")
}
};
JSONC was also included in the bundle as an object. After the build, I changed port in config.toml to 4000. bun toml-app.ts then printed localhost 4000, but the bundle dist/toml-app.js still printed localhost 3000. If you change the TOML, you need to rebuild.
When I built toml-dates.ts, which imports only schedule by name, the bundle contained only the schedule object. The toml-app.ts bundle was 813 bytes, and the toml-dates.ts bundle was 379 bytes. Unused tables are left out of the bundle.
If TOML contains dates or times, the bundle includes code such as Temporal.Instant.from(...). When I built with --target=node and ran it on Node.js 22, toml-app.ts stopped with ReferenceError: Temporal is not defined as soon as it loaded, even though it doesn't use schedule directly. This is because the default import config includes schedule. If you write the date in quotes, as in releasedAt = "2026-11-21T09:00:00+09:00", it becomes a plain string, and in the bundle for Node.js 22 it could be read with new Date(data.releasedAt).
In TypeScript, named imports need type declarations
@types/bun declares *.toml and *.jsonc files as modules of type any (the per-extension module declarations in @types/bun). This makes default imports pass, but named imports did not. The main settings in the tsconfig.json I used were "moduleResolution": "bundler", "types": ["bun"], and "strict": true.
$ ./node_modules/.bin/tsc -p .
conf-attr.ts(2,18): error TS2307: Cannot find module './app.conf' or its corresponding type declarations.
jsonc-app.ts(2,20): error TS2305: Module '"*.jsonc"' has no exported member 'experimental'.
toml-app.ts(2,18): error TS2305: Module '"*.toml"' has no exported member 'server'.
toml-app.ts(2,26): error TS2305: Module '"*.toml"' has no exported member 'plans'.
toml-dates.ts(2,10): error TS2305: Module '"*.toml"' has no exported member 'schedule'.
Code that runs under Bun can still produce errors in type checking. So I used TypeScript's allowArbitraryExtensions and placed config.d.toml.ts, which declares the types for config.toml, in the same folder. With this setting on, when config.toml is imported, config.d.toml.ts is used as its type declaration (allowArbitraryExtensions in the TypeScript tsconfig reference).
// config.d.toml.ts: type for config.toml
type Plan = { name: string; price: number };
export const title: string;
export const maxItems: number;
export const server: { host: string; port: number };
export const database: { host: string; port: number; tags: string[] };
export const plans: Plan[];
export const schedule: {
releasedAt: Temporal.Instant;
openDate: Temporal.PlainDate;
opens: Temporal.PlainTime;
};
declare const config: {
title: typeof title;
maxItems: typeof maxItems;
server: typeof server;
database: typeof database;
plans: typeof plans;
schedule: typeof schedule;
};
export default config;
The first tsc -p . errors above were produced after I had added "allowArbitraryExtensions": true to compilerOptions in tsconfig.json, but before I placed the type declaration files. After I added config.d.toml.ts and settings.d.jsonc.ts, which I wrote in the same form for settings.jsonc, the TS2305 errors went away. With types in place, code that assigns server.port (a number) to a string variable stopped with error TS2322: Type 'number' is not assignable to type 'string'. When I turned off allowArbitraryExtensions, config.d.toml.ts was no longer used, and the TS2305 errors came back.
On the other hand, bun does not check types when it runs. The code that assigned server.port to a string variable ran under bun and printed 3000 without any problem.
The TS2307 error for app.conf that remained after adding the type declarations comes from the import with the import attribute with { type: "toml" }. The official documentation says that with TypeScript 7.1 or later, @types/bun determines the type from the type attribute, and with earlier versions it determines the type from the extension alone (the official note on import attributes and TypeScript versions). The examples in that note are type: "text" and type: "sqlite", and it does not describe the types for type: "toml". With TypeScript 7.0.2, which I used for testing, this produced Cannot find module './app.conf'.





