JavaScript YAML Parser: Parse YAML in Node.js

Learn how to parse YAML in JavaScript and Node.js with yaml and js-yaml, read YAML files, convert YAML to JSON, and handle parser errors safely.

Published

Updated

Read time 4 min read

Reviewed byDeepak Prasad

JavaScript YAML Parser: Parse YAML in Node.js

YAML is commonly used for configuration files, CI/CD pipelines, Kubernetes manifests, and application settings. JavaScript does not include a built-in YAML parser, so the usual way to parse YAML in JavaScript or Node.js is to install a package such as yaml or js-yaml, read the YAML string or file, and convert it into a normal JavaScript object.

This guide shows practical ways to parse YAML in Node.js, read a .yml file, convert YAML to JSON, and handle invalid YAML without crashing your script.

Environment: Node.js v20.18.2. After each runnable snippet, the following paragraph states the expected console output (order and values).


Method 1: Parse YAML with the yaml Package

The yaml package is a modern JavaScript YAML parser and stringifier. Use it when you want a maintained YAML parser for Node.js applications and frontend build workflows.

Install the package first:

bash
npm install yaml

Parse a YAML string with YAML.parse():

javascript
const YAML = require("yaml");

const yamlText = `
name: Ana
skills:
  - JavaScript
  - YAML
active: true
`;

const parsed = YAML.parse(yamlText);

console.log("yaml-name:", parsed.name);
console.log("yaml-skills:", parsed.skills.join(","));
Output

You should see 2 lines, in order: yaml-name: Ana, yaml-skills: JavaScript,YAML.

After parsing, parsed is a regular JavaScript object. You can access YAML keys with dot notation, loop over arrays, or pass the object to other functions.


Method 2: Parse YAML with js-yaml

js-yaml is another widely used YAML parser for JavaScript. It provides load() for reading a single YAML document and dump() for converting JavaScript objects back to YAML.

Install it with npm:

bash
npm install js-yaml

Example:

javascript
const jsYaml = require("js-yaml");

const yamlText = `
name: Ana
skills:
  - JavaScript
  - YAML
active: true
`;

const config = jsYaml.load(yamlText);

console.log("js-yaml-active:", config.active);
Output

You should see one line logging js-yaml-active: true.

Use js-yaml when your project already depends on it or when you need compatibility with existing Node.js tooling that uses the same package.


Method 3: Read and Parse a YAML File in Node.js

For a real Node.js project, YAML is usually stored in a .yml or .yaml file. Read the file with fs.readFileSync() or fs.promises.readFile(), then pass the file content to the parser.

Create a file named config.yml:

yaml
app:
  name: dashboard
  port: 3000
features:
  login: true
  reports: true

Parse the YAML file:

javascript
const fs = require("fs");
const YAML = require("yaml");

const file = fs.readFileSync("config.yml", "utf8");
const config = YAML.parse(file);

console.log(config.app.name);
console.log(config.features.login);
Output

Sample output:

You should see 2 lines, in order: dashboard, true.

This is the typical pattern for reading YAML configuration in Node.js. Keep file reading separate from parsing so parser errors and file path errors are easier to debug.


Method 4: Convert YAML to JSON in JavaScript

Once YAML is parsed into an object, use JSON.stringify() to convert it into JSON text.

javascript
const YAML = require("yaml");

const yamlText = `
name: Ana
active: true
roles:
  - admin
  - editor
`;

const obj = YAML.parse(yamlText);
const json = JSON.stringify(obj, null, 2);

console.log(json);
Output

Sample output:

json
{
  "name": "Ana",
  "active": true,
  "roles": [
    "admin",
    "editor"
  ]
}

This is useful when an API expects JSON but your source configuration is written in YAML.


Handle Invalid YAML Safely

A JavaScript YAML parser throws an error when the YAML syntax is invalid. Wrap parsing in try...catch when user-edited YAML files or external YAML content may be malformed.

javascript
const YAML = require("yaml");

try {
  YAML.parse("name: Ana\n  bad-indent: true");
} catch (error) {
  console.log("YAML error:", error.message.split("\n")[0]);
}
Output

When building CLI tools or config loaders, include the file name in your own error message so users know which YAML file needs correction.


Common Questions About JavaScript YAML Parsing

Does JavaScript have a built-in YAML parser?

No. JavaScript has built-in JSON support with JSON.parse(), but YAML parsing requires a third-party package such as yaml or js-yaml.

Should I use yaml or js-yaml?

Both are valid choices. Use yaml for a modern parser and stringifier with a clean API. Use js-yaml when you already depend on it or need compatibility with existing Node.js scripts that call load() and dump().

Can I parse YAML in the browser?

Yes, but you normally bundle the parser with Vite, Webpack, Rollup, or another frontend tool. Browser JavaScript does not expose fs, so reading local YAML files is different from reading YAML in Node.js.


Summary

To parse YAML in JavaScript, install a YAML parser, read the YAML string or file, and convert it into a JavaScript object. The yaml package works well for modern Node.js projects, while js-yaml remains common in existing scripts and tools. For production code, handle invalid YAML with try...catch, keep file reading separate from YAML parsing, and convert parsed YAML to JSON only after validating the resulting object.


Official Documentation

Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs …