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

btrconf

v0.1.0

Published

Better (& simpler) node.js config loader.

Downloads

86

Readme

btrconf Build Status

Better (read: simpler) node.js config loader.

While there are other node.js config loaders out there, they force you to split your config into separate files. A default config file and then another per environment. For a small application (or microservice) it's often overkill to have your config spread across 3-5 different files.

btrconf is of the opinion that, for small projects, one config file is not just sufficient, but better. If you find your config file getting too large, split it into multiple files by configuration type (eg. database config, external service auth, etc.).

Usage

btrconf uses the NODE_ENV environment variable to determine which config it should load (default: 'development').

Given a config file ./config.js:

var env = process.env;
module.exports = {
  default: {
    port: env.PORT || 8080,
    errors: env.DISPLAY_ERRORS || true,
    db: {
      client: 'pg'
    }
  },
  test: {
    db: {
      connection: 'postgres://localhost:5432/testdb'
    }
  },
  development: {
    db: {
      connection: 'postgres://localhost:5432/devdb'
    }
  },
  production: {
    db: {
      connection: env.DB_URI,
    },
    errors: false
  }
}

You might have a server.js that looks something like:

var btrconf = require('btrconf');
var express = require('express');
var knex = require('knex');

var config = btrconf.load('./config');
var db = knex(config.db);
var server = express();

/* Set up your routes, etc. */

server.listen(config.port);

Typescript

This module comes with it's own (tiny) type definitions. The only real difference when you're using Typescript is that you'll need an interface that describes the structure of your config file.

To load the config file in the example above, your Typescript would look something like:

import * as btrconf from 'btrconf';

interface AppConfig {
  port: number;
  errors: boolean;
  db: {
    client: string;
    connection: string;
  };
}

let config = btrconf.load<AppConfig>('./config');
/* ... */

At the moment, there is no guarantee that the object is read from your config file matches your interface. If this is something you're interested in, raise an issue as I have a solution for this; I just haven't had time to implement it.

Caveats

Hey, we said it was better, not perfect ;)

Requiring btrconf from multiple files.

Since we're requiring files relative to your module, from all the way down in your node_modules/, we use module.parent.filename to determine the name of the file you are requiring btrconf from. We then load the specified config file relative to your that file.

This means that if you require('btrconf') from multiple files, module.parent.filename will refer to the first file to require btrconf. In this case, you can pass an absolute filename using:

var config = btrconf.load(require.resolve('./config'));

Due to the above, btrconf is currently not suitable for use in other npm modules, sorry!