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

config-path

v2.1.0

Published

Loads a config based on environment variables CONFIG and NODE_ENV.

Downloads

6

Readme

config-path

Loads a config based on a file (overridable by the environment variable CONFIG) and the Node.js environment (environment variable NODE_ENV).

npm install config-path --save

Config file

This module exports a function(filename) which loads a config file.

filename is the path to the YAML file. If it points to a directory, it loads the config file config.yml inside this directory. If it's undefined, it defaults to the process's current working directory

In any case, the value of filename can be overriden by the environment variable CONFIG.

If the config file does not exist, ENOENT is thrown.

Example

Given the file ./super-website/app.js:

var config = require('config-path')();
var express = require('express');

var app = express();
...
app.listen(config.listen);

the following command loads ./config.yml:

node ./super-website/app.js

the following command loads ./super-website/config.yml:

cd super-website && node ./app.js

the following command loads /var/www/configs/super-website.yml:

CONFIG=/var/www/configs/super-website.yml node ./super-website/app.js

Config format

This package loads YAML files. Since JSON is a subset of YAML, JSON files are also accepted. And because it's still YAML, JSON files can contain comments too!

The file must describe an associative array where the keys are the available environments. NODE_ENV defines which environment is chosen. If NODE_ENV is undefined, defaults to development. An Error is thrown if the environment is not available.

As part of the YAML specs, you can use anchors and references to specify variables across environments.

Example

development: &development
  app:
    title: Super website
  listen: 3000
  redis: &redis
    host: localhost
    port: 6379

production:
  <<: *development
  listen: 80
  redis:
    <<: *redis
    host: databases.lan

will deliver when NODE_ENV is undefined or is development:

{
	app: {
		title: "Super website",
	},
	listen: 3000,
	redis: {
		host: "localhost",
		port: 6379,
	},
}

and when NODE_ENV is production:

{
	app: {
		title: "Super website",
	},
	listen: 80,
	redis: {
		host: "databases.lan",
		port: 6379,
	},
}

Test environment

In order to use a test environment when running the tests, you could write (for instance with mocha):

"scripts": {
  "test": "NODE_ENV=test mocha"
}

Sadly this won't work on Windows. However, you can use grunt-mocha-cli that can run mocha with custom environment variables.

Moreover, you can use the following pattern in order to automatically append -test to the running NODE_ENV. This is especially useful if you have several staging environments (e.g. user-acceptance, load-testing, demo...).

grunt.initConfig({
	mochacli: {
		options: {
			env: {
				NODE_ENV: (process.env.NODE_ENV || 'development') + '-test'
			}
		}
	}
});

Config required from multiple files

You shouldn't require this module several times for the same config, because it will read and parse the config file every time. Instead, create the following file ./config.js:

module.exports = require('config-path')(PATH_TO_CONFIG_YML);

and in your files, write:

var config = require('./config');

License

Copyright (c) 2014 Bloutiouf aka Jonathan Giroux

MIT licence