Easily read JSON files from a directory. Great for configuration.

Usage no npm install needed!

<script type="module">
  import jsoncfg from 'https://cdn.skypack.dev/jsoncfg';


Node.js - jsoncfg

Easily read JSON files from a directory. Great for configuration.


I store configuration data in JSON files. It's handy to load them up and parse them with one method. Also, it includes undefined field support from field.


npm install jsoncfg



Let's assume that you have the following files structure:

├── database.json
├── malformed.json
├── shopping.json
├── weird\ name2.json
├── weird-name.json
└── weird_name3.json

let's assume that database.json looks like:

    "development": {
        "name": "myapp_development",
        "host": "",
        "port": 27017
    "test": {
        "name": "myapp_test",
        "host": "",
        "port": 27017
    "production": {
        "name": "myapp_production",
        "host": "myserver.com",
        "port": 27017
var jsoncfg = require('jsoncfg');

jsoncfg.loadFiles('./config', function(err, files, errInfo) {
  if (err) { //this is an error object, see errInfo for all errors for each file
    console.log(errInfo['malformed']) //could not parse 'malformed.json'

  console.log(files.database.production.host) //"myserver.com"

  var dbCfg = files.database

  //can also use string notation to avoid the following:
  var host = (dbCfg && dbCfg.production && dbCfg.production.host)
  host = dbCfg.get('production.host')

  //use ':' if you prefer
  host = dbCfg.get('production:host')

  //or treat everything as a field, including the configuration file
  host = files.get('database:production.host')

  //you can also create fields
  dbCfg.set('production.location.country', 'US') //returns old value if overwriting or value, or `undefined` if new
  console.log(dbCfg.production.location.country) //'US'

loadFiles(dir, [cache], callback)


loadFilesSync(dir, [cache])


Config Files

For a full configuration package, see: fnoc which is built on this.


(MIT License)

Copyright 2013, JP Richardson jprichardson@gmail.com