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

prefer

v0.5.11

Published

Configuration management for NodeJS

Downloads

97

Readme

What is it? Build Status

Prefer is a node library for helping you manage application configurations.

It provides a set of interfaces which provide standard methods for reading arbitrary project configuration data. This can vary from simple cases like JSON, to more complicated examples - such as retreiving configuration data from a database.

How do I use it?

Firstly, you'll want to install the module. This can be done easily with npm.

npm install prefer

Prefer is fairly simple to use. A basic use case might be that you have the following JSON configuration in settings.json:

{
  "auth": {
    "username": "user",
    "password": "pass"
  }
}

You can load these settings with the following code using promises:

function configure (value) {
    // value will be set to "user" at this point.
}

require('prefer').load('settings')
    .then(function (configurator) {
        configurator.get('auth.username').then(configure);
    });

If you prefer to use callbacks, you can also use them as you wish throughout the prefer APIs:

// Get a Configurator object which we can use to retrieve settings.
require('prefer').load('settings', function (err, configurator) {
    if (err !== null) { throw err; }

    configurator.get('auth.username', function (err, value) {
        // value will be set to "user" at this point.
    });
});

You will notice that prefer only required 'settings' as the filename. It should always be given without a path or extension, because prefer takes care of looking through the filesystem for configuration files. On both Unix and Windows systems, it will look in all of the standard folders, as well as some conventional places where people like to put their configurations.

Ordering matters, so having a file in ./settings.json as well as another in /etc/settings.json is still reliable. The configuration in ./settings.json will be used first. Prefer doesn't care what format your user writes your settings in, so they can also use settings.yaml, settings.xml, settings.cson, or any other supported format.

If you prefer to look in specific places, you can always pass an options object as the second argument to prefer.load, and provide it the files.searchPaths setting as an array of locations for prefer to look in. Here's an example:

var prefer = require('prefer'),
    options = {
        files: {
            searchPaths: ['./etc', '.']
        }
    };

// Get a Configurator object which we can use to retrieve settings.
prefer.load('settings', options, someFunction);

Supported configuration formats

Along with being fully configurable to support any arbitrary data source you'd like, the following types of data can immediately be used as configuration formats upon installation of prefer:

  • CoffeeScript
  • INI
  • JSON (using json5)
  • CSON
  • XML
  • YAML

Why asyncronous?

A lot of configuration tools prefer to provide a blocking method of retrieving a project's configuration, in order to supply a more-simple method of getting the configuration. One goal of prefer is to make sure that we aren't limited to specific use-cases - and some projects require real-time, dynamic updating of their configuration. Prefer provides all of it's interfaces in an asyncronous manner in order to provide that possibility without the requirement that those actions are blocking.