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

@rmarscher/config

v1.0.4

Published

Standard way to initialize a config object.

Downloads

2

Readme

Node Config

Returns a config object using environment vars. If a .env file exists at the root of the project, it will load that to set environment vars. Returns the nconf instance which has get/set/merge. https://github.com/indexzero/nconf

Here is the order of the merging done by nconf:

  1. Defaults
  2. Environment vars
  3. Memory
  4. Overrides

By default, only defaults are set and environment vars are retrieved.

The calling application can update the defaults, set settings to the memory store using config.set() or config.merge() and also set up overrides which will always take precedence over memory or environment vars.

Note that with nconf, a colon - ':' - is the default key separator and it's possible to get/set deep into objects by using a colon in the key.

i.e.

config.get( 'mongo:uri' ); // 'mongodb://localhost/test'

Setup

It is expected to load this module before anything else in your app. It will setup New Relic monitoring if process.env.NEW_RELIC_HOME is set. Note that this must be an actual environment var and not part of a .env file because we want to be able to instrument even the dotenv module.

import config from '@rmarscher/config';

That config object is ready to go with our standard defaults and could be just used out of the box.

We recommend at least setting the app name for the log:

config.set( 'log:name', 'my-app' );

Another way to setup the config is to use the config.setup(defaults, overrides) method. This will completely clear out all of the existing config and set it up again. The defaults will be merged with the standard defaults.

Environment Vars

Use two underscores __ to nest paths and create sub-objects. So an envrionment var database__host=127.0.0.1 would cause require( '@rmarscher/config' ).get( 'database' ) to return { host: '127.0.0.1' }.

It's expected to load this before anything else in the app. It will also read environment variables in from a .env file using the dotenv package.

One thing to be careful about with environment variables is everything is treated as a string. You can get a falsey value by setting the environment variable to an empty string:

express__cors=

Therefore, it's important that anything that consumes a config setting handle casting it appropriately. For booleans, it's recommended to use just an if ( config.get( 'somevar' ) ) so truthy/falsey values will work.

Setting express__cors=false or express__cors=0 in a .env or environment var will not evaluate to false. It's very important to remember this.

Similarly, numeric values could be strings if set from env vars, so you must use something like parseInt() or Number() to make sure to convert strings to numbers.

Standard Defaults

Here are the standard defaults currently in use. The express, newrelic and port config are required for the @rmarscher/express-init module. The log config is required for the @rmarscher/log module.

The paths config is used by the requireLocal() function in the @rmarscher/globals module. Given requireLocal() can't work with ES6 modules, we may phase that out. Although, it might be still fairly handy until we can figure out if the ES6 import paths to check can be configured.

    var standardDefaults = {
        express: {
            cors: false,
            paths: {
                'static': path.resolve( process.cwd(), './public' ),
                'views': path.resolve( process.cwd(), './app/views' ),
            },
            trustProxy: true,
            viewEngine: 'hbs'
        },
        log: {
            file: null,
            level: 'info',
            name: 'log', // maybe there's a way to get the calling app's name from package.json
            stream: process.stdout
        },
        mongo: {
            uri: 'mongodb://localhost/test',
            log: false,
            options: {
                db: {
                    safe: true
                },
                socketOptions: {
                    keepAlive: 1
                }
            }
        },
        newrelic: newrelic ? true : false,
        paths: {
            'code': path.resolve( process.cwd(), './app' ),
            'public': path.resolve( process.cwd(), './public' ),
            'tests': path.resolve( process.cwd(), './test' ),
        },
        port: process.env.PORT || 8080,
        // NodeMailer SMTP Transport settings
        smtp: {
            // host: 'smtp.mandrillapp.com',
            port: 587,
            // auth: {
            //     user: '',
            //     pass: ''
            // },
            // default is 5, trying a higher value
            'maxConnections': 15,
            'maxMessages': Infinity
        },
        versionPrefix: '/v1'
    };