Read/ write ini files in javascript

This post talks about reading and writing the ini files with the following examples

Read ini file into memory

Adding ini dependency

Create a nodejs application using the npm init -y command. Please install the npm ini library using the npm install ini command.

This will be included in package.json as follows

  "dependencies": {
    "ini": "^1.3.5"
  }

for yarn and bower-based projects, please add the following dependency

yarn add ini
bower install ini

Sample INI data example file

Following is the ini example file which is used as a base for parse, reading, and writing the data to it.

;config.toml
; Created by Franc
[Author]
name=Franc
updateDate=20-03-2020.

[database]
driverclass   = com.mysql.jdbc.Driver
dbName        = mydatabase
port          = 3306
username      = root
password      =

Read/parse INI file in JavaScript

Import fs and ini library into the application requires a statement

const fs = require('fs');
const ini = require('ini');
const config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
console.log(config);

and Output is

{
  Author: {
    name: 'Franc',
    updateDate: '20-03-2020.'
  },
  database: {
    driverclass: 'com.mysql.jdbc.Driver',
    dbName: 'mydatabase',
    port: '3306',
    username: 'root',
    password: ''
  }
}

Write INI file in JavaScript