YAML 1.2 parser and serializer for JavaScript environments with complete specification support
npx @tessl/cli install tessl/npm-js-yaml@4.1.0js-yaml is a comprehensive YAML 1.2 parser and serializer for JavaScript environments, implementing the complete YAML specification with high performance and compatibility. It offers both loading (parsing) and dumping (serialization) functionality for YAML documents, supporting single and multi-document parsing, various schema types, and extensive configuration options for both parsing and serialization behaviors.
npm install js-yamlconst yaml = require('js-yaml');For ES modules:
import * as yaml from 'js-yaml';
import { load, dump } from 'js-yaml';const fs = require('fs');
const yaml = require('js-yaml');
// Parse YAML from string
try {
const doc = yaml.load('hello: world\ncount: 42');
console.log(doc); // { hello: 'world', count: 42 }
} catch (e) {
console.log(e);
}
// Parse YAML from file
try {
const doc = yaml.load(fs.readFileSync('/path/to/file.yml', 'utf8'));
console.log(doc);
} catch (e) {
console.log(e);
}
// Serialize object to YAML
const obj = { hello: 'world', count: 42 };
const yamlString = yaml.dump(obj);
console.log(yamlString);js-yaml is built around several key components:
Core functionality for parsing YAML documents into JavaScript objects, with support for single documents, multi-document streams, and various parsing options.
function load(input, options);
function loadAll(input, iterator, options);Converts JavaScript objects to YAML strings with extensive formatting and style options for clean, readable output.
function dump(input, options);Extensible schema system for controlling which YAML types are supported during parsing and serialization, with built-in schemas and support for custom types.
class Schema extends Function;
class Type extends Function;
const FAILSAFE_SCHEMA, JSON_SCHEMA, CORE_SCHEMA, DEFAULT_SCHEMA;
const types: {
binary: Type, float: Type, map: Type, null: Type, pairs: Type,
set: Type, timestamp: Type, bool: Type, int: Type, merge: Type,
omap: Type, seq: Type, str: Type
};Comprehensive error handling with detailed position information and helpful error messages for debugging YAML parsing issues.
class YAMLException extends Error;Built-in CLI tool for inspecting and processing YAML files from the command line.
js-yaml [-h] [-v] [-c] [-t] fileLegacy functions from js-yaml v3 that now throw helpful migration errors.
function safeLoad(input, options); // Use load() instead
function safeLoadAll(input, options); // Use loadAll() instead
function safeDump(input, options); // Use dump() instead// Load options
interface LoadOptions {
filename?: string;
onWarning?: (warning: YAMLException) => void;
schema?: Schema;
json?: boolean;
}
// Dump options
interface DumpOptions {
indent?: number;
noArrayIndent?: boolean;
skipInvalid?: boolean;
flowLevel?: number;
styles?: object;
schema?: Schema;
sortKeys?: boolean | ((a: string, b: string) => number);
lineWidth?: number;
noRefs?: boolean;
noCompatMode?: boolean;
condenseFlow?: boolean;
quotingType?: '"' | "'";
forceQuotes?: boolean;
replacer?: (key: string, value: any) => any;
}