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
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.
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
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.
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,
},
}
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'
}
}
}
});
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');
Copyright (c) 2014 Bloutiouf aka Jonathan Giroux