Skip to content

Modifying the Configuration Files Parser

Loren West edited this page May 16, 2023 · 12 revisions

Using NODE_CONFIG_PARSER you can provide node-config with an alternative path from which to load the parser definitions and file types resolution order.

You'll need to create a new file which imports from config/parser and exports the same Parser object after applying modifications.

Modifying the default parser is done using the following methods:

const Parser = require('config/parser');
// Modify using Parser API
module.exports = Parser;

It may look simple (because it is!) but with it you can define any parser your heart desires.

Parser Examples

import Parser from 'config/parser';

// coffeescript parser
import 'coffeescript/register';
// once registered, coffee files are loaded as any other js file 
Parser.setParser('coffee', Parser.getParser('js'));

// custom yaml parser
import { safeLoad } from 'yaml-parser';
function yamlParser(filename, content) {
  return safeLoad(content);
}
// note that when you override supported types
// their resolution order doesn't change
Parser.setParser('yml', yamlParser);
Parser.setParser('yaml', yamlParser);

// Hocon parser
import hoconParser from 'hocon-parser';
Parser.setParser('hocon', (filename, content) => hoconParser(content));

// override json parser so json files are parsed as json5
Parser.setParser('json', Parser.getParser('json5'));

export default Parser;  // must be exported!

getParser(extension)

Gets the parser of a given extension.

param type description
extension string name of the corresponding file extension
const Parser = require('config/parser');
const xmlParser = Parser.getParser('xml');
Parser.setParser('rss', xmlParser);
module.exports = Parser;

setParser(extension, parser)

Sets the parser for a given extension.

Both filename and content are provided to support both use cases, but normally you will only use one of the two. filename to file types which are required, such as coffee and ts files, while content can be used by any string parser available to you. (ini, hocon or you own custom parser, you name it!)

Note that when adding support for a previously unknown file extension it is automatically appended to the files resolution order, giving it priority over all other file types in the resolution order, while existing exntesions keep their previous resolution order.

setFileOrder can be used to override default file resolution order heuristics.

param type description
extension string name of the corresponding file extension
parser function provide a parser function, which expects filename and content as arguments, and returns an object containing parsed results to be merged.

param type description
filename string an absolute path to the configuration file
content string string content of the configuration file
const ini = require('ini');
const Parser = require('config/parser');
Parser.setParser('ini', (filename, content) => ini.parse(content));
module.exports = Parser;

getFilesOrder([extension])

Gets information about the order in which file types are resulted and merged. Returns the entire files order array, unless called with an extension argument in which case it will return the extension's index in the files order array. This can be used with setFilesOrder to make specific changes.

param type description
extension string file extension name
const ini = require('ini');
const Parser = require('config/parser');
Parser.getFilesOrder(); // ['js', 'json', 'json5', 'yaml', ... ]
module.exports = Parser;

setFilesOrder(order)
setFilesOrder(extension, index)

Sets which extensions will be support by node-config (automatically loaded) and their resolution order.

setFilesOrder(order)

param type description
order array an array containing file extensions in the desired order of resolution
const Parser = require('config/parser');
Parser.getFilesOrder(); // ['js', 'json', 'json5', 'yaml', ... ]
Parser.setFilesOrder(['yaml', 'yml', 'json', 'json5', 'js']);  // file types not present will not be parsed
module.exports = Parser;

setFilesOrder(extension, index)

param type description
extension string file extension name
index integer an index to move extension to in the files order array
// make sure json5 files are resulted before json files
Parser.setFilesOrder('json5', Parser.getFilesOrder('json')); 

parse(filename, content)

Used internally to match and execute a parser according to filename's extracted extension.

param type description
filename string an absolute path to the configuration file
content string string content of the configuration file