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

confn

v1.0.2

Published

A simple to implement config management node js library. Setup dev, staging, production, etc. config for your node app effortlessly.

Downloads

21

Readme

Usage

It's very simple to setup and use confn . confn also reads from the environment by default.

Sample:

const Conf = require('confn');
Conf.set('HOST', '127.0.0.1');
Conf.set('PORT', 9337);

console.log('HOST: ', Conf.get('HOST'));
console.log('PORT: ', Conf.get('PORT'));
console.log('USER: ', Conf.get('USER'));

Now if you run the script above:

USER=root node script.js

The output will be:

HOST:  127.0.0.1
PORT:  9337
USER:  root

Intialize config

const Conf = require('confn');
const config = {
  defaults: {
    HOST:  '127.0.0.1',
    PORT:  9337,
    USER:  'root'
    OBJECT: {
    	RANDOM: 'random'
    }
  },
  staging: {
    HOST: 'https://staging.co'
  },
  production: {
    HOST: 'https://production.co',
    PORT: 9335
  }
}

Conf.init(config);
console.log(Conf.get().HOST);
  • Now if you run the script above:
NODE_ENV=staging node script.js
  • The output will be:
https://staging.co
  • Note after you have called Conf.init(config) you can fetch the keys from any file by using const { KEY1, KEY2 } = require('confn').get()
Description:

confn reads the key-value pairs from defaults key of the config passed in Conf.init(config). If there is any key that is in defaults and also in the environment then the value in the enviroment is given precedence. This means that if you run the above script with HOST=localhost node script.js then the output would be localhost and not 127.0.0.1.

  • Note: Keys present inside the staging or production key. i.e. the objects whose keys are used for a specific NODE_ENV have precedence over both environment and defaults keys. In the above example, in the production object the host and port both will override the defaults and environment keys.

set, override and hardSet

  • You can use the above three methods to set keys.
const Conf = require('confn');
Conf.set('HOST', 1);
Conf.override('HOST', 12);
Conf.hardSet('HOST', 123);
  • The difference between these methods is that though you can use set to update / add a key, value pair. You cannot set a key if it has been overriden. i.e. if Conf.override(key, value) has been used on that specific key. However if you really want to update that key then you can use Conf.hardSet(key, value) to update that key.
  • The above three methods are used in Conf.init(config). First the environment keys are set by using the override method. And after that the keys under defaults are set. (Which is why the environment has more precedence than defaults). After that the staging, production etc. NODE_ENV key, value pairs are set with the hardSet method hence they have the highest precedence.

Stores

  • confn by default uses a store env for config management. If you want to then you can also add more stores that you might want to use.

Sample code:

const Conf = require('confn');
const config = {
  defaults: {
    HOST: '127.0.0.1',
    PORT: 9337,
    USER: 'root',
  },
  staging: {
    HOST: 'https://staging.co',
  },
  production: {
    HOST: 'https://production.co',
    PORT: 9335,
  },
};
Conf.addStore('memory');
Conf.init(config, 'memory');
console.log(Conf.get(null, 'memory'));
  • The code above will create a different store name memory where all the configuration keys will be stored. Please note that stores don't share any key, value information amongst each other apart from the environment key, value pairs.
  • Sample output of above code:
  .
  .
  SPACESHIP_VERSION: '3.11.2',
  SPACESHIP_ROOT: '/Users/root/.nvm/versions/node/v14.6.0/lib/node_modules/spaceship-prompt',
  P9K_TTY: 'old',
  _: '/Users/root/.nvm/versions/node/v14.6.0/bin/node',
  HOST: '127.0.0.1',
  PORT: 9337
}