Skip to main content
In Bun, YAML is a first-class citizen alongside JSON and TOML. You can:
  • Parse YAML strings with Bun.YAML.parse
  • import & require YAML files as modules at runtime (including hot reloading & watch mode support)
  • import & require YAML files in frontend apps via bun’s bundler

Conformance

Bun’s YAML parser currently passes over 90% of the official YAML test suite. While we’re actively working on reaching 100% conformance, the current implementation covers the vast majority of real-world use cases. The parser is written in Rust for optimal performance and is continuously being improved.

Runtime API

Bun.YAML.parse()

Parse a YAML string into a JavaScript object.

Multi-document YAML

When parsing YAML with multiple documents (separated by ---), Bun.YAML.parse() returns an array:

Supported YAML Features

Bun’s YAML parser supports the full YAML 1.2 specification, including:
  • Scalars: strings, numbers, booleans, null values
  • Collections: sequences (arrays) and mappings (objects)
  • Anchors and Aliases: reusable nodes with & and *
  • Tags: type hints like !!str, !!int, !!float, !!bool, !!null
  • Multi-line strings: literal (|) and folded (>) scalars
  • Comments: using #
  • Directives: %YAML and %TAG

Error Handling

Bun.YAML.parse() throws a SyntaxError if the YAML is invalid:

Module Import

ES Modules

You can import YAML files directly as ES modules. The YAML content is parsed and made available as both default and named exports:
config.yaml

Default Import

app.ts

Named Imports

You can destructure top-level YAML properties as named imports:
app.ts
Or combine both:
app.ts

CommonJS

YAML files can also be required in CommonJS:
app.ts

Hot Reloading with YAML

One of the most powerful features of Bun’s YAML support is hot reloading. When you run your application with bun --hot, changes to YAML files are automatically detected and reloaded without closing connections

Configuration Hot Reloading

config.yaml
server.ts
Run with hot reloading:
terminal
Now when you modify config.yaml, the changes are immediately reflected in your running application. This is perfect for:
  • Adjusting configuration during development
  • Testing different settings without restarts
  • Live debugging with configuration changes
  • Feature flag toggling

Configuration Management

Environment-Based Configuration

YAML excels at managing configuration across different environments:
config.yaml
app.ts

Feature Flags Configuration

features.yaml
feature-flags.ts

Database Configuration

database.yaml
db.ts

Bundler Integration

When you import YAML files in your application and bundle it with Bun, the YAML is parsed at build time and included as a JavaScript module:
terminal
This means:
  • Zero runtime YAML parsing overhead in production
  • Smaller bundle sizes
  • Tree-shaking support for unused configuration (named imports)

Dynamic Imports

YAML files can be dynamically imported, useful for loading configuration on demand:
Load configuration based on environment