- Parse YAML strings with
Bun.YAML.parseand stringify objects withBun.YAML.stringify import&requireYAML files as modules at runtime (including hot reloading & watch mode support)import&requireYAML 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:
%YAMLand%TAG
Error Handling
Bun.YAML.parse() throws a SyntaxError if the YAML is invalid:
Bun.YAML.stringify()
Convert a JavaScript object into a YAML string.
JSON.stringify():
The
replacer parameter is currently not implemented and has no effect.space parameter controls indentation (default: 2). With space: 0, YAML outputs in compact flow style on a single line:
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:TypeScript
Unlike JSON files, TypeScript doesn’t automatically type YAML imports. Add type definitions by creating a.d.ts file with the same name as your YAML file:
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 restarting your application or closing existing 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
Generating YAML Files
You can useBun.YAML.stringify() to programmatically create YAML configuration files. It handles complex data structures including nested objects, arrays, and objects within arrays. When you reference the same object multiple times, YAML automatically creates anchors (&) and aliases (*) to avoid duplication:
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