- 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 Zig 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
Named Imports
You can destructure top-level YAML properties as named imports:CommonJS
YAML files can also be required in CommonJS:Hot Reloading with YAML
One of the most powerful features of Bun’s YAML support is hot reloading. When you run your application withbun --hot
, changes to YAML files are automatically detected and reloaded without closing connections
Configuration Hot Reloading
config.yaml
terminal
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
Feature Flags Configuration
features.yaml
Database Configuration
database.yaml
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
- 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