Parsing YAML files in Node.js using the js-yaml module

The following example shows how you can parse .yaml files in Node.js using the js-yaml module.

YAML is a human friendly data serialization standard. Basically, YAML is to JSON, as Markdown is to HTML. It removes the need for double-quotes and excessive curly brackets.

For more information on YAML, see For an excellent online YAML-to-JSON converter, see or

The easiest way to install the js-yaml module is to use npm using the following command in the Terminal/command line: npm install js-yaml

First, create a YAML file, config.yaml, and enter the following text:

username: admin
password: guest
- mon
- tue
- wed
- thu
- fri

Next, create the main JavaScript file, app.js, and enter the following text:

#!/usr/bin/env node

// Include our 'js-yaml' module.
var yaml = require("js-yaml");

/* Import the 'config.yaml' file in the current directory, convert it to JSON, and store it in the `config` variable. */
var config = require("./config.yaml");

/* Pretty-print the JSON object out to the stdout/console. */
console.log(JSON.stringify(config, null, "    "));

If you run the previous code from the Terminal (node ./app.js) you can see that the YAML is converted to JSON:

    "username": "admin",
    "password": "guest",
    "days": [

1 comment

  1. Interesting, but it fails to show how to actually use the settings defined in the yaml. For example, how would you get the username and password fields to utilize them in the express app??

Leave a Reply

Your email address will not be published.