DEV Community

Mr Smithnen
Mr Smithnen

Posted on • Edited on • Originally published at Medium

How to Use YINI Config Files in a Node.js App (with Real Examples)

πŸ™‹β€β™‚οΈ Why I Wrote This

You know that moment when you're knee-deep in a side project and you just want a clean config file?

Something easier on the eyes than JSON, but more structured than old-school INI?

That was me β€” and that's how YINI started.

Not to reinvent configuration formats, but just to make one that I wouldn't dread editing.

In this post, I'll walk you through how to actually use YINI in a Node.js app, with real examples pulled straight from my own parser tests and tooling setup. It's simple, flexible, and (hopefully) a bit fun.


πŸ“¦ Installing the Parser

First, install the YINI parser from npm:

npm install yini-parser
Enter fullscreen mode Exit fullscreen mode

πŸ§ͺ Parsing a Basic Config

Let's start with a simple YINI config:

^ App
title = 'My App Title'
items = 25
isDarkTheme = true
Enter fullscreen mode Exit fullscreen mode

Here's how you load it in your app:

import YINI from 'yini-parser';

const config = YINI.parse(`
    ^ App
    title = 'My App Title'
    items = 25
    isDarkTheme = true
`);

console.log(config.App.title); // 'My App Title'
Enter fullscreen mode Exit fullscreen mode

YINI requires strings to be quoted for clarity.
Numbers, booleans, and nulls are parsed correctly too β€” without weird surprises.

The above config corresponds to this JavaScript object:

{
  App: {
    title: 'My App Title',
    items: 25,
    isDarkTheme: true
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Loading from a File

In most real projects, you'll want to load your config from a file.
yini-parser provides a helper for that: YINI.parseFile(..).

import YINI from 'yini-parser';

const config = YINI.parseFile('./config.yini');

console.log(config.App.title); // e.g., 'My App Title'
Enter fullscreen mode Exit fullscreen mode

This works with .yini files, and supports options like strict mode or metadata.

Under the hood, parseFile(..) just reads the file and passes it to YINI.parse(..). Simple and clean.

πŸ’‘ Structured Config with Nested Sections

Sometimes your config grows β€” and when it does, structure helps.
YINI supports nested sections (similar as to in Markdown) but with using caret-based indentation:

@yini

^ App
name = "Nested Example"
version = "1.0.0"
debug = OFF

    ^^ Database
    host = "db.example.com"
    port = 3306
    user = "appuser"
    password = "dbpassword"

        ^^^ Pool
        min = 2
        max = 10
        idleTimeout = 300

    ^^ Logging
    level = "info"
    logToFile = ON
    filePath = "./logs/app.log"

/END
Enter fullscreen mode Exit fullscreen mode

You can parse this in strict mode like this:

const config = YINI.parse(yiniString, true); // true = strict mode
Enter fullscreen mode Exit fullscreen mode

The resulting object:

{
  App: {
    name: 'Nested Example',
    version: '1.0.0',
    debug: false,
    Database: {
      host: 'db.example.com',
      port: 3306,
      user: 'appuser',
      password: 'dbpassword',
      Pool: { min: 2, max: 10, idleTimeout: 300 }
    },
    Logging: { level: 'info', logToFile: true, filePath: './logs/app.log' }
  }
}
Enter fullscreen mode Exit fullscreen mode

πŸ›  Optional Metadata

Want more than just key/values?
You can enable metadata mode to get some stats and some other info β€” useful for diagnostics, etc.

const configWithMeta = YINI.parse(yiniString, false, 'auto', true);
Enter fullscreen mode Exit fullscreen mode

πŸ—¨οΈ Comments β€” Your Way

YINI supports a variety of comment styles:

// This is a comment
# So is this
-- And this

/*
Block comments
are supported too
*/
Enter fullscreen mode Exit fullscreen mode

βœ… Recap

YINI isn't trying to be better than JSON, TOML, or YAML.
It just tries to be clear, consistent, and pleasant to use β€” especially when you want nesting without indentation sensitivity, or strict validation without a mess of brackets.

If you like working with structured configs in Node.js and want something that's:

  • Human-friendly
  • Easy to read
  • Tooling-ready
  • Just structured enough…

πŸ”— Links

– Marko
Creator of YINI

Note: This article was originally published on Medium.

Top comments (0)