npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modular-config

v1.0.0

Published

Simplistic library for loading configurations from different file formats

Downloads

4

Readme

modular-config

A simplistic library for synchronously loading read-only configurations from a variety of file formats.

For many projects, the config library is sufficient for all configuration needs. But if you like other formats beyond JSON, or prefer less automatic loading behavior, this package offers another path.

Usage

npm install modular-config
// With just a file name, lookups will occur at process.cwd() + '/config'
var configA = require('modular-config').load('application.toml');


// Absolute paths are supported
var configB = require('modular-config').load('/path/to/file.json');


// Or you can set another configuration search directory
var conf = require('modular-config');
conf.directory('/var/tmp');
var configC = conf.load('oddly_placed.toml');


// File extensions are required unless you specify a parser
var configD = require('modular-config').load('generic', 'toml');

How It Works

This library is structured on the idea that all configurations will be read from disk synchronously at application start time. This is often an acceptable assumption, since many things won't happen until configuration information is available.

When a configuration is successfully loaded, it is turned into an object tree, made read-only, cached, then returned. Subsequent requests for the same file will returned the previously parsed configuration. This means that configurations cannot be reloaded without restarting the application.

Configuration files may be loaded with an absolute file path or by file name in a configurable lookup directory (which defaults to process.cwd() + '/config').

All errors throw exceptions. Though this is a bit extreme, it fits with the use-case of upfront loading. If configuration information isn't available, it is assumed that the application cannot reasonably start.

Out of the box, TOML (.toml) and JSON (.json, .js) configuration formats are supported. Other formats may easily be added via the register() function. An extension and parser function must be supplied, with the latter returning the parsed object (from raw string input).

// Enable YAML config parsing
var conf = require('modular-config')
  , yaml = require('js-yaml');

conf.register('yaml', function (input) {
  try {
    return yaml.safeLoad(input);
  } catch (e) {
    throw new Error('Cannot parse YAML configuration file');
  }
});

var configd = require('modular-config').load('configD.yaml');

Existing parsers may be overridden on a per-extension basis. However, remember that the parsing results are cached, so any new parsers must return an object tree. Trying to be fancy by returning a Promise (or similar) can result in unexpected behavior.