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

get-config

v0.6.15

Published

Automagically builds a config object used throughout an application.

Downloads

131

Readme

NPM NPM

get-config

get-config is a Node.js library automagically building a config object used throughout an application.

// Asynchronous (Async/Await)
try {
  const config = await require('get-config').load(`${__dirname}/config`);
} catch (err) {...}

// Asynchronous (Promise)
require('get-config').load(`${__dirname}/config`)
  .then(config => {...})
  .catch(err => {...});

// Asynchronous (Callback)
require('get-config').load(`${__dirname}/config`, (err, config) => {...});

// Synchronous
try {
  const config = require('get-config').loadSync(`${__dirname}/config`);
} catch (err) {...}
  • Both promises and callback (via Bluebird) styles are supported.
  • Synchronous version is also supported. (.loadSync())
  • get-env is used to parse process.env.NODE_ENV.

Supported Formats

  • INI: .ini, .cfg, .conf → (requires npm install ini)
  • JS: .js
  • JSON: .json
  • TOML: .toml → (requires npm install toml)
  • XML: .xml → (requires npm install xml2json)
  • YAML: .yaml, .yml → (requires npm install js-yaml)

How It Works

Take a look at the example structure.

It assumes you have a separate directory somewhere in your project that is devoted to static config values that are further manipulated and used by an application. It reads all files ending with one of the supported format extensions then constructs a config object for you using filenames with their extension dropped (ex: server.json or server.yaml becomes server) as the key and the loaded content from the file as value during the construction of the config object. You can have one or more of these files with any choice of file format among the supported list, and you can mix them as well.

For example, if you placed both client.json and server.yaml files in config/, it would return a config object looking like this:

{
  client: <content-from-client.json>,
  server: <content-from-server.yaml>
}

It also assumes you have an optional override directory (usually to override default values with environment-specific values based on process.env.NODE_ENV). The override directory path is a relative path to the (default) config directory. If you pass dev as the override directory, the library will read all the config files under config/dev/ in the same way explained above for the (default) config directory, then the values will be merged with the default config object.

For example, if you placed both client.json and server.yaml files in config/ and client.xml in config/dev, it would return a config object looking like this when you run your application in the dev environment (it would return the object same as the above example for the rest of environments):

{
  client: <content-from-client.json merged with content-from-config/client.xml>,
  server: <content-from-server.yaml>
}

Imagine you defined multiple environment names within your application and you created override directories for each of these environments under config/ with environment-specific config values organized into separate files (using the environment name as the override directory name). All you need to do now is to let the library know what environment you are in by passing the environment name as override directory path to let the library take care of environment-specific config object loading.

Check out get-env library for delegating NODE_ENV environment variable loading and environment definitions.

Installation

$ npm install get-config

You also need to install parser for your choice of formats:

  • INI: npm install ini
  • JSON: included
  • TOML: npm install toml
  • XML: npm install xml2json
  • YAML: npm install js-yaml

Usage

const getConfig = require('get-config');
const env = getConfig.env();  // alias to "get-env"

// Option 1: Async/Await
try {
  const config = await getConfig.load(`${__dirname}/config`, env);
} catch (err) {...}

// Option 2: Promise
getConfig.load(`${__dirname}/config`, env)
  .then(config => {...})
  .catch(err => {...});

// Option 3: Callback
getConfig.load(`${__dirname}/config`, env, (err, config) => {...});

// Option 4: Synchronous
try {
  const config = getConfig.loadSync(`${__dirname}/config`, env);
} catch (err) {...}

env is an optional parameter. If you do not pass the env value, it internally calls getConfig.env() (alias to "get-env") then uses that value for you.

It is recommended to stay with get-env library's convention (dev and prod) to structure your config directory.

Credits

Special thanks to:

See the contributors.

License

Analytics