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

@medolino/node-config

v0.0.1

Published

NodeJS env config helper

Downloads

2

Readme

@medolino/node-config

JavaScript Style Guide npm

@medolino/node-config is a helper tool that deals with env configuration in an organized way.

How It Works

When calling initialization function, user provides a configuration parameter settings, which are used to validate, cast and organize parsed process.env values in a tree structure. After initialization getConfig helper function is exposed and can be used to retrieve specific configuration value or set of values.

As detenv is used under the hood, environment variables can also be stored in a .env file.

Install

Install locally using npm:

npm i @medolino/node-config --save

Usage

Create a .env file in the root of your project and store desired env variables:

API_HOST = 0.0.0.0
API_PORT = 3000
DB_HOST = 0.0.0.0
DB_PORT = 27018
LOGGER_LEVEL = debug

Initialize config and use getConfig function to get specific config values:

const nodeConfig = require('@medolino/node-config')

const paramConfig = {
  api: {
    server: {
      host: {
        envVarName: 'API_HOST', // env variable name
        type: nodeConfig.valueDataTypes.STRING, // data type to cast to
        required: false, // is env value required
        defaultValue: 'localhost' // use this value, when env value not provided
      },
      port: {
        envVarName: 'API_PORT',
        type: nodeConfig.valueDataTypes.INTEGER,
        required: false,
        defaultValue: 4000
      }
    },
    db: {
      host: {
        envVarName: 'DB_HOST',
        type: nodeConfig.valueDataTypes.STRING,
        required: false,
        defaultValue: 'localhost'
      },
      port: {
        envVarName: 'DB_PORT',
        type: nodeConfig.valueDataTypes.INTEGER,
        required: false,
        defaultValue: 27017
      }
    },
    logger: {
      level: {
        envVarName: 'LOGGER_LEVEL',
        type: nodeConfig.valueDataTypes.STRING,
        required: true,
        defaultValue: 'info',
        enum: ['debug', 'info', 'fatal']
      }
    }
  }
}

const { getConfig } = nodeConfig({ paramConfig })

const apiConfig = getConfig('api')

console.log(apiConfig)

/*
{
  "server": {
    "host": "0.0.0.0",
    "port": 3000
  },
  "db": {
    "host": "0.0.0.0",
    "port": 27017
  },
  "logger": {
    "level": "debug"
  }
}
*/

const dbConfig = getConfig('api.db')
console.log(dbConfig)

/*
{
  "host": "0.0.0.0",
  "port": 27017
}
*/

const loggerEnabled = getConfig('api.logger.level')
console.log(loggerEnabled)

/*
"debug"
*/

Overriding values from .env

You can override any env value, defined in .env using a parameter on the CLI command line when starting the application:

LOGGER_LEVEL=fatal npm run start

Supported value data types

During initialization, env values are casted to desired data type.

A list of supported data types:

  • boolean,
  • integer,
  • string,
  • array of strings,
  • array of integers

See: nodeConfig.valueDataTypes

Documentation