INI File Parser and Serializer

6.0.0 · active · verified Sun Apr 19

The `ini` package provides a robust encoder and decoder for INI file formats in Node.js environments. As of version 6.0.0, it targets Node.js `^20.17.0 || >=22.9.0`. The library is actively maintained, with frequent major version bumps primarily driven by updates to supported Node.js engine ranges. Key features include parsing INI strings into nested JavaScript objects, handling section-less items as globals, and supporting bracketed arrays (e.g., `key[] = value`). It also offers comprehensive serialization with options for whitespace, alignment, sorting of sections and keys, platform-specific line endings, and custom section identifiers. It differentiates itself by offering fine-grained control over output format and careful handling of common INI parsing quirks. The project maintains a healthy release cadence, with at least one new version released in the past 12 months.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates reading INI data, parsing it into a JavaScript object, modifying properties (including nested sections and arrays), and then stringifying the object back into an INI formatted string with a custom top-level section.

import { writeFile , readFile } from 'node:fs/promises'
import { stringify , parse } from 'ini'

const iniContent = `
; This comment is being ignored
scope = global

[database]
user = dbuser
password = dbpassword
database = use_this_database

[paths.default]
datadir = /var/lib/data
array[] = first value
array[] = second value
array[] = third value
`

async function manageIni() {
  // Simulate reading INI file content
  let text = iniContent;

  // Parse text data to object
  const config = parse(text)

  // Modify data object
  config.scope = 'local'
  config.database.database = 'use_another_database'
  config.paths.default.tmpdir = '/tmp'
  delete config.paths.default.datadir
  if (config.paths.default.array && Array.isArray(config.paths.default.array)) {
    config.paths.default.array.push('fourth value')
  }

  // Stringify data object with a section prefix
  text = stringify(config, {
    section : 'newSection',
    whitespace: true // Optional: Add spaces around = for readability
  })

  console.log('Modified INI content:\n', text)
  // In a real app, you'd write this back to a file:
  // await writeFile(`./Modified.ini`,text)
}

manageIni().catch(console.error);

view raw JSON →