YAML Parser and Stringifier

2.8.3 · active · verified Sun Apr 19

The `yaml` library is a robust and definitive JavaScript parser and stringifier for YAML, supporting both YAML 1.1 and 1.2 specifications and all common data schemas. It currently maintains a stable version 2.8.3, with an active release cadence that regularly introduces new features and bug fixes. A v3.0.0-0 prerelease is also available under the `next` tag, though it is not yet stable for production use and is not recommended for production environments. Key differentiators include passing all `yaml-test-suite` tests, its ability to parse any string input without throwing errors (extracting as much YAML as possible), and comprehensive support for parsing, modifying, and writing YAML comments and blank lines. It boasts no external dependencies and runs across various JavaScript environments including Node.js (requiring `>= 14.6`), modern browsers, Deno, Bun, and Cloudflare Workers, shipping with full TypeScript type definitions (minimum TS 3.9).

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates parsing multi-document YAML strings, modifying values and adding new items using the Document API, and then stringifying the result. It also shows parsing a single document with `parseDocument` and direct AST manipulation.

import { parse, stringify, Document, YAMLMap, Scalar } from 'yaml';

const yamlString = `
# My shopping list
- item: Apples
  quantity: 2
  notes: Green ones

---
# My second shopping list
- item: Milk
  quantity: 1
  unit: gallon
`;

// Parse a multi-document YAML string, keeping CST nodes for better comment preservation
const documents = parse(yamlString, { keepCstNodes: true }) as Document[];

// Access and modify the first document
const firstDoc = documents[0];
if (firstDoc && firstDoc.contents instanceof Array) { // Assuming a sequence at root
  const appleItem = firstDoc.contents[0];
  if (appleItem instanceof YAMLMap) {
    appleItem.set('quantity', 3); // Change quantity
    appleItem.set('notes', 'Honeycrisp preferred'); // Update notes
  }
}

// Access and modify the second document
const secondDoc = documents[1];
if (secondDoc && secondDoc.contents instanceof Array) {
  secondDoc.contents.push(new YAMLMap());
  const newItem = secondDoc.contents[secondDoc.contents.length - 1] as YAMLMap;
  newItem.set('item', 'Bread');
  newItem.set('quantity', 1);
  newItem.set('type', 'whole wheat');
}

// Stringify the modified documents back to YAML
const modifiedYaml = documents.map(doc => stringify(doc)).join('\n---\n');

console.log('Original YAML:\n', yamlString);
console.log('\nModified YAML:\n', modifiedYaml);

// Example of parsing a single document with parseDocument and accessing AST
const singleDocString = `
user: John Doe
roles:
  - admin
  - editor
active: true
`;

const doc = parseDocument(singleDocString);

if (doc.contents instanceof YAMLMap) {
  const userNode = doc.contents.get('user');
  if (userNode instanceof Scalar) {
    console.log(`\nUser from single document: ${userNode.value}`);
  }
  doc.contents.set('status', 'online');
}

console.log('\nModified Single Document with new status:\n', doc.toString());

view raw JSON →