js-yaml
YAML parser and emitter for JavaScript where load is safe by default since version 4.
Official Documentation ↗v4. Check the official docs for the latest API surface.Installation
npm install js-yamlpnpm add js-yamlMethods
No methods match this filter.
YAML parser and emitter for JavaScript where load is safe by default since version 4.
Overview
js-yaml parses and emits YAML for JavaScript with a safe default schema since v4. load turns documents into objects, loadAll handles — separated streams, and dump serializes back with formatting options that keep files diffable.
Schema options tighten or relax the accepted tag vocabulary, and errors include line and column positions. It remains the most widely used YAML engine in the Node ecosystem for configuration files and build pipelines.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
yaml.load— Parses YAML safely into JavaScript objects using the default core schema.yaml.loadAll— Parses multi-document streams separated by — into an array of objects.yaml.dump— Serializes objects back to YAML with formatting control for diffs and humans.schema options— Chooses the tag vocabulary; JSON_SCHEMA is strictest and safest for untrusted input.
Usage Notes
- Pin dump options, indent, lineWidth, noRefs, wherever output lands in git.
- Use JSON_SCHEMA when parsing fully untrusted configuration.
- Validate the parsed structure with a schema library before use.
Common Pitfalls
- Dumping strips all comments, so round-trips lose human annotations.
- Unquoted version-like strings can parse as numbers, quote them.
- Anchors and aliases in input collapse to shared references in the output object.
FAQ
- Is yaml.load safe on untrusted input?
- Since v4, load uses a safe default and the old load with unsafe tags is gone, safeLoad is deprecated because load now is safe. For defense in depth on untrusted files, restrict further with JSON_SCHEMA, which limits types to the JSON subset.
- Why do my yes/no values become booleans?
- YAML 1.1 core schema treats unquoted yes, no, on, off as booleans in several implementations, and js-yaml follows the 1.1 vocabulary for compatibility. Quote such strings or validate types after parsing.
- How do I keep dump output stable for git?
- Pin indent and lineWidth, set noRefs to avoid anchors, and avoid sortKeys unless you want alphabetical churn. Round-tripping comments is not supported, emitted YAML loses them.
Report a problem with this API docOpens a pre-filled issue on GitHub.