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

@dskrepps/config

v0.1.0

Published

Simple but powerful Node.js config loader

Downloads

2

Readme

@dskrepps/config

Simple but powerful config loader which can read any config format supported by coherent. It will attempt to load various common files in order of priority listed below. Also provided are methods has, get, and set for accessing nested objects using Dotty and saveLocalChanges to save values changed with set.

This module is written with brevity and readability in mind so you can easily fork your own version for your own unique config loading criteria. But if you'd like to use it as is:

NPM

Usage

const getConfig = require('@dskrepps/config');
var config = getConfig();

// Generate & save a new session key if it's missing from config
if (!config.has('app.secrets.sessionKey')) {
	let newSessionKey = require('crypto').randomBytes(48).toString('hex');
	config.set('app.secrets.sessionKey', newSessionKey);
	// Save the key to config/local.json, which should be in .gitignore
	config.saveLocalChanges();
}

// A quick modular Express app
var app = require('express')();
app.set('config', config);

// Pass app (and thus app.config) to each module in array config.modules
config.modules.forEach( modulePath => require(modulePath)(app) );

app.listen( config.port, config.host, () => console.log(
	'%s is listening at %s:%d in %s mode',
	config.get('app.name') || 'App',
	config.host, config.port, config.env
) );

Order of Priority

Your config options are merged in this order using deep-extend and files are read using coherent, how you see here:

  • Initial values (next section)
  • First argument passed to this module
  • coherent(config.configDir + 'default')
  • coherent(config.configDir + config.env)
  • if (!config.ignoreLocalConfig) coherent(config.configDir + config.localConfig)
  • envNodeConfig = JSON.parse(process.env.NODE_CONFIG)
  • argv = minimist(process.argv.slice(2)) /* Command line options */
  • for (file of config.additionalConfigs) coherent(config.configDir + file)

Initial Values

  • configDir: __dirname+'/../../config/' /* 'config' directory adjacent to node_modules */
  • env: argv.env || envNodeConfig.env || process.env.NODE_ENV || 'development'
  • localConfig: 'local.json'
  • additionalConfigs: []
  • allowSaveChanges: true

Methods

config = require('@dskrepps/config')([configToExtend])

Load the config files in the order of priority above. configToExtend will extend the intial config values before being by the other configs.

config.loadConfig(fileName)

Syncronously Loads another config file from the config directory to extend onto the same config object. Fails silently if none found.

config.has(str)

Check if an object has a property using Dotty's exist. Returns true or false.

config.get(str)

Get a value. See Dotty's get. Returns the value or undefined.

config.set(str, value[, dontSave=false])

Sets a value, deeply if necessary using Dotty's put. If dontSave is true it won't remember the change when saveLocalChanges is called.

config.saveLocalChanges(callback)

Will save changed values made with the set method to the config.localConfig file. Does nothing if there have been no modifications, or if allowSaveChanges is not truthy, or if localConfig is not truthy. Takes a callback which is passed an Error on failure.

License

MIT