in Bun: Native Imports and the Bun.JSON5 API
Bun is the one mainstream JavaScript runtime that reads JSON5 without a package: import a .json5 file, or call Bun.JSON5.parse().
The feature arrived in Bun v1.3.7 on January 27, 2026, was rewritten from Zig to Rust in Bun 1.4, and today parses and stringifies, imports and bundles. It is smaller than the json5 npm package, though: no reviver, no replacer, no quote option.
This page covers the exact API, the conformance claim and who makes it, what the bundler does with a JSON5 import, the TypeScript types that ship with Bun, a migration checklist from the npm package, and the gotchas.
TL;DR: On Bun 1.3.7 or newer, import config from "./config.json5" and Bun.JSON5.parse(text) work with no install. Bun.JSON5.stringify(value, null, space) writes it back. Bun states that its parser passes 100% of the official test suite. Bring the json5 npm package only if you need a reviver, a replacer or the quote option, or if the same code must also run on Node.js.
Which Bun Version, and How It Got Here
Bun announced Bun.JSON5.parse() and Bun.JSON5.stringify() in the release notes for Bun v1.3.7, dated January 27, 2026, under the heading "Native JSON5 Support", together with the ability to import a .json5 file directly. The change itself was a pull request by Dylan Conway (oven-sh/bun #26439), opened January 25 and merged January 26, 2026; it closed a long-standing feature request, issue #3175. The pull request lists 317 unit tests of its own plus 113 cases taken from the official JSON5 test suite.
The first implementation was written in Zig, and the Bun 1.3.14 documentation, which ships inside the bun-types package, still said so: "The parser is written in Zig for optimal performance." Bun 1.4 (August 20, 2026) rewrote the runtime in Rust, and the current documentation page, which is headed Bun v1.4.5 (released November 19, 2026), says the parser "is written in Rust". If you read an older tutorial that says Zig, it was correct when it was written. The documented API in the 1.4.5 type package is the same one.
| Bun release | Date | What it means for JSON5 |
|---|---|---|
| v1.3.7 | January 27, 2026 | Bun.JSON5 and .json5 imports first ship |
| 1.4 | August 20, 2026 | Runtime rewritten in Rust; JSON5 support carried over |
| v1.4.5 | November 19, 2026 | The version the current documentation page is written for |
Version floor for your project: require Bun 1.3.7 or newer. Older releases predate the feature. Pin the version in your CI image or setup step, and check bun --version there.
The Bun.JSON5 API
There are two functions, available on the global Bun object and as a named export from the bun module. Both spellings are the same object.
Bun.JSON5.parse(input: string): unknown
Parses a JSON5 string into a JavaScript value. Bun's documentation lists what it accepts: both comment forms, trailing commas in objects and arrays, unquoted keys (any ECMAScript 5.1 identifier), single- and double-quoted strings, backslash line continuations inside strings, hexadecimal numbers, leading and trailing decimal points, Infinity and NaN with either sign, and an explicit plus sign. Invalid input throws a SyntaxError. The declared signature takes one argument.
import { JSON5 } from "bun";
const result = JSON5.parse(`{
// This is a comment
name: 'my-app',
version: '1.0.0', // trailing comma is allowed
hex: 0xDEADbeef,
half: .5,
infinity: Infinity,
}`);The sample is the example in Bun's own type declarations. We could not run it: Bun is not installed on our test machine, so every Bun snippet on this page is quoted from the documentation, the release notes or the shipped type package rather than executed by us.
Bun.JSON5.stringify(input: unknown, replacer?: undefined | null, space?: string | number): string | undefined
Converts a value to JSON5. Output is compact unless you pass space: a number of spaces per level (clamped between 0 and 10) or a string whose first 10 characters become the indent. Unlike JSON.stringify, it keeps Infinity, -Infinity and NaN as literals instead of writing null. It returns undefined for undefined, functions and symbols. The type declaration says the replacer argument is "Not supported", and its type allows only undefined or null.
console.log(JSON5.stringify({ name: "my-app", version: "1.0.0" }));
// {name:'my-app',version:'1.0.0'}
console.log(JSON5.stringify({ a: 1, b: 2 }, null, 2));
// {
// a: 1,
// b: 2,
// }The compact output is the example on Bun's documentation page, and the indented output is the example in its type declarations. Indented output ends each level with a trailing comma, as the declaration states. The documentation page and the pull request show single-quoted strings, while the type declaration's own example shows {a:1,b:"two"}; see the gotchas.
Round trip with a comment
As with every JSON5 library that is not a round-trip editor, a comment is discarded the moment the text is parsed, so parse-then-stringify returns the data but not the annotation. If a tool must edit a file and keep its comments, see the implementations directory for other libraries.
const data = Bun.JSON5.parse("{ /* keep me */ a: 1 }");
// { a: 1 } - the comment is gone
await Bun.write("out.json5", Bun.JSON5.stringify(data, null, 2));What is not in the documented API
A reviver for parse and a replacer for stringify are the two things JSON.parse and JSON.stringify have that Bun's functions do not. On October 24, 2026 the author of the original parser opened a pull request (oven-sh/bun #46747) to add both. It was closed without being merged the same day, and the 1.4.5 type declaration still marks the replacer "Not supported", so treat both as unsupported and check the current documentation before relying on either. stringify has no equivalent of the npm package's quote option either.
Importing .json5 Files
Bun's module resolver treats .json5 as a data module, the same way it treats .json, .toml and .yaml. The file is parsed when it is loaded and the result is the module's default export; top-level properties are also available as named exports. No loader flag, no bunfig.toml entry and no import attribute is needed, although Bun's file-types documentation also shows the explicit form import data from "./data.txt" with { type: "json5" } for a file with another extension.
// config.json5
{
// Database configuration
database: {
host: 'localhost',
port: 5432,
},
tags: ['web', 'api',],
}
// app.ts
import config from "./config.json5"; // the whole object
import { database } from "./config.json5"; // a top-level property
const cjs = require("./config.json5"); // CommonJS works too
const { default: dyn } = await import("./config.json5"); The documentation covers each form: default import, named imports, require() and dynamic import() where the parsed value is on default. Bun's guide "Import a JSON5 file" shows the same thing in a few lines.
Hot reloading
Run a server with bun --hot server.ts and an edit to an imported .json5 file is picked up without restarting the process, which makes a commented config file a pleasant thing to tune against a running service. Bun's documentation lists watch mode as supported as well.
An import is a code path, not a file read. Anything you import is resolved relative to the importing module and can be bundled into your output. Use an import for config that ships with your code. For a file chosen at run time, such as a user-supplied path, read it with Bun.file(path).text() and pass it to Bun.JSON5.parse(), so a bad file raises an error you can catch and report.
What the Bundler Does With It
bun build parses an imported JSON5 file at build time and emits it as a JavaScript module, so the production bundle carries a plain object literal and no parser. Bun's documentation names three consequences: no JSON5 parsing at run time in production, smaller bundles, and tree shaking of named imports that nothing uses. The same applies to frontend code that Bun bundles for the browser.
bun build app.ts --outdir=dist The practical rule: a .json5 file you import is frozen into the bundle at build time. Editing it after the build changes nothing in the shipped output. If the file must stay editable by operators after deployment, read it at run time instead.
Other bundlers need a plugin or a loader to understand the format; see the guides for Vite, webpack, Rollup and esbuild.
The Conformance Claim, Attributed
Bun's documentation states that its parser passes 100% of the official JSON5 test suite and links a translated copy of that suite in the Bun repository that lists every case. That is Bun's own statement, made by the people who wrote the parser, and we have not re-run it. What the pull request itself shows is narrower and checkable: 113 tests drawn from the official suite, plus 317 of Bun's own.
Passing the shared suite is a good sign and not a guarantee, because the suite covers the grammar and a handful of error cases, not every edge of your documents. The suite and what it measures are described on the conformance page, and where Bun sits among other parsers is on the implementations page.
The grammar Bun implements is the one in the specification grammar.
TypeScript Types
Install bun-types (or @types/bun) and the types arrive with it. We downloaded bun-types 1.4.5, the latest on npm today, and read the declarations:
parse(input: string): unknown, so you must narrow or validate the result before using it.stringify(input: unknown, replacer?: undefined | null, space?: string | number): string | undefined.declare module "*.json5"types an imported file asany, and a matchingdeclare module "*" with { type: "json5" }declaration inimport-attributes.d.tscovers the import-attribute form.
Because an imported file is typed any and parse returns unknown, the type system will not catch a wrong key. Give the data a shape yourself, ideally with a runtime check, as in the schema validation tutorial:
import raw from "./config.json5";
interface AppConfig {
name: string;
port: number;
}
function isAppConfig(v: unknown): v is AppConfig {
return typeof v === "object" && v !== null
&& typeof (v as any).name === "string"
&& typeof (v as any).port === "number";
}
if (!isAppConfig(raw)) throw new Error("config.json5 has the wrong shape");
const config: AppConfig = raw;More on TypeScript with this format is in the TypeScript guide and the TypeScript errors page.
Migrating From the json5 npm Package
The package in the JavaScript guide is the reference implementation, and Bun's functions are deliberately close to it. For plain config loading the swap is mechanical. Check each row before you delete the dependency.
| Feature | json5 npm package | Bun.JSON5 |
|---|---|---|
parse(text) | ||
parse(text, reviver) | ||
stringify(value, null, space) | ||
stringify with a replacer function or array | ||
stringify options object with quote | ||
Error carries lineNumber and columnNumber | Not documented; the documentation says only that a SyntaxError is thrown | |
Imports .json5 files natively | Only through the package's Node.js require hook | |
| Runs on Node.js and Deno |
A safe swap
// before
import JSON5 from "json5";
const config = JSON5.parse(await Bun.file("config.json5").text());
// after: read and parse as before
const config = Bun.JSON5.parse(await Bun.file("config.json5").text());
// or, for config that ships with the code, just import it
import config from "./config.json5";- Search the code for a second argument to
parse, a second or third argument tostringify, and anyquoteoption. Keep the npm package where any of them appear. - Search for
catchblocks that readlineNumberorcolumnNumber. Those are properties of the npm package's errors. - Compare the written output if another program reads it: the quote character in output differs between sources (see the gotchas).
- If the code also runs on Node.js or Deno, keep the npm package there;
Bun.JSON5does not exist on those runtimes. Our guides for Node.js and Deno show the options.
Gotchas
1. Sources disagree about output quotes
The documentation page's examples and the original pull request show single-quoted output, matching the npm package. The type declaration's own example writes {a:1,b:"two"} with double quotes. If another program parses your output, parse it, not its quote style, and test the exact Bun version you ship on.
2. Older Bun lacks the feature
The feature shipped in v1.3.7, so a CI image pinned to an earlier release has no Bun.JSON5. Check bun --version in CI, not just on your laptop.
3. NaN and Infinity survive
Both functions accept and emit them, which JSON.stringify does not. A value that goes through JSON5 and then through JSON.stringify becomes null.
4. Imports are frozen at build time
Bundled output holds the parsed object. Operators cannot edit the file in production and see a change. Read it at run time if they must.
5. No streaming and no comments on output
The API is whole-string in, whole-string out, and comments are not preserved. For large inputs see the performance tutorial, and for untrusted input the security tutorial.
6. package.json is still JSON
Support for a .json5 import does not change what Bun reads for its own manifest. A feature request for a package.json5 file exists in the Bun repository (issue #26552); we did not verify its status, so keep package.json as plain JSON.
JSON5 in Bun: Common Questions
Which Bun version added JSON5?
Bun v1.3.7, released January 27, 2026. Bun 1.4 (August 20, 2026) later rewrote the runtime in Rust.
Do I still need the json5 npm package?
Not for plain parsing, writing and importing on Bun. Yes for a reviver, a replacer, the quote option, parse error line numbers, or code that must also run on Node.js or Deno.
Is Bun's parser conformant?
Bun states that it passes 100% of the official test suite. See what the suite checks and decide how much weight that statement carries for your files.
Does Bun support JSONC or YAML too?
Its documentation places JSON5 beside JSON, TOML and YAML as first-class formats, and a .jsonc declaration appears in the type package. For how JSON5 compares, see JSONC and YAML.
What does a bad file throw?
A SyntaxError. Wrap runtime parses in try/catch; an import of a broken file fails when the module is loaded.
Sources
(11)
- Bun documentation: JSON5 (page header: Bun v1.4.5)
- Bun guide: Import a JSON5 file
- Bun v1.3.7 release notes, January 27, 2026
- Bun blog index (release dates; Bun 1.4 Rust rewrite)
- oven-sh/bun pull request 26439: native JSON5 parser
- oven-sh/bun issue 3175: Implement JSON5 support as a module
- oven-sh/bun pull request 46747: reviver and replacer (closed, not merged)
- bun-types 1.4.5 on npm (bun.d.ts, extensions.d.ts, import-attributes.d.ts)
- json5/json5-tests: the official JSON5 test suite
- Bun documentation: file types and loaders
- The JSON5 specification
Last reviewed: November 29, 2026.
JavaScript
Python
TypeScript
Node.js
Rust
Java
C#
Ruby
Kotlin
Lua
Babel
Vite
webpack
ESLint
Prettier
VS Code
JetBrains