read-ini
v1.1.1
Published
Read and parse INI or ENV files
Readme
read-ini
A simple reader and parser of .ini (or .env) text / files for NodeJS.
Install
$ npm i read-iniUsage
The API contains just two functions:
- readIni - takes
.ini/.env-like text content, parses it and returns a JSON object. - readIniFile - synchronously reads a text file and then forwards to readIni.
The library supports sections, with aliases, but without nesting.
INI file example
# full-line comment can start with #
SHARED_VALUE = some text
; full-line comment can start with ;
[database]
DB_HOST = localhost
DB_PORT = 123Reading the file above...
import {readIniFile} from 'read-ini';
readIniFile('./file.ini'); //=> JSON objectOutput:
{
SHARED_VALUE: 'some text',
database: {
DB_HOST: 'localhost',
DB_PORT: '123'
}
}Value-type conversion
You can pass in an optional callback, as the second parameter, to convert values.
readIniFile('./file.ini', ({key, value, section}) => {
if (key === 'DB_PORT') {
return parseInt(value); // convert to integer
}
return value; // else return the value
});Output:
{
SHARED_VALUE: 'some text',
database: {
DB_HOST: 'localhost',
DB_PORT: 123
}
}Section Aliases
Optional section aliases are supported: [section "alias"], and those simply replace the section name.
Global Sections
Section name global is reserved (case-insensitive), to inject variables into the global scope from anywhere inside an
INI file. The same works for any section with global as alias.
Environment Variables
To set environment variables from the output, you can use this helper:
function setEnvironmentVars(vars: { [name: string]: any }): void {
for (const [name, value] of Object.entries(vars)) {
if (typeof value !== 'object') {
process.env[name] = value;
}
}
}