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

yml-config-reader

v0.1.4

Published

YAML Config Reader

Downloads

1

Readme

yml-config-reader - YAML Configuration Reader for Node.js

Node.js Package Coverage Status

This plugin provides a way to configure your node project based on YAML files. The configuration reader itself is really small since almost everything is done by js-yaml or lodash. You can use it as an alternative to dotenv with the advantage that you can provide default settings and use the more powerful YAML format.

Installation

$ npm install yml-config-reader --save-dev

Usage

Create a file config.default.yml with your configuration. Example:

app1:
    serviceUrl: app1.api.com
db:
    name: test-db
stage: qa

To read this config file, you need the following javascript code.

const configReader = require('yml-config-reader')

const config = configReader.getByFiles('config.default.yml')
console.log(config.db.name) // prints test-db

The method getByFiles can take an arbitrary number of config files that are merged together with values in later files taking precedence. Example: getByFiles('config.default.yml', 'config.qa.yml')

As an alternative you can pass an environment variable to your node application which then can be used to pick up the according config files. Example:

$ STAGE=test node yourscript.js
const configReader = require('yml-config-reader')

const config = configReader.getByEnv(process.env.STAGE)
console.log(config.db.name) // prints test-db

The command getByEnv takes a parameter environment and uses this one to search for the file config.${environment}.yml. This method always checks if a file called config.default.yml exists and uses configuration values that are defined in that file as defaults.

You can also specify a folder name if you wish to store your config files separately. Example:

const configReader = require('yml-config-reader')

configReader.getByEnv(process.env.STAGE, 'config') // uses the 'config' folder
configReader.getByFiles('config/config.default.yml', 'config/config.qa.yml')

Compound / Nested Variables

It is possible to reuse existing properties in a config file as values for other properties. You can use the syntax ${varName} if you wish that the string ${varName} shall be substituted with the actual value of the property varName. Example:

baseUrl: app.company.com

app1:
    restUrl: app1.${baseUrl}

If you use multiple configuration files this substitution takes all properties across all files into account.

Usage with webpack

This plugin can also be used with webpack, for example if you want to use global variables in your web application that shall be stored in YAML configuration files. Here you can find an example for your webpack.config.js.

const webpack = require('webpack')
const configReader = require('yml-config-reader')

module.exports = (env) => {
  const config = configReader.getByEnv(env.stage || 'test')

  return {
    plugins: [
      new webpack.DefinePlugin({
        "process.env": JSON.stringify(config)
      })
    ]
  }
}

The config map is then available in your script files by using the variable process.env.

This example uses webpack environment variables to pass a proper value to the function getByEnv. You can pass this value directly via the command line with npm start -- --env.stage=qa.