What is rc?
The rc npm package is a configuration loader for Node.js applications. It allows you to configure your applications with files, environment variables, and command-line arguments. This package is particularly useful for creating flexible applications that can be easily configured without changing the codebase.
What are rc's main functionalities?
Loading configuration from a file
This feature allows you to load configuration for your application named 'appname' from a file. If the file does not exist, it falls back to the default configuration provided.
const config = require('rc')('appname', { defaultConfig: 'defaultValue' });
Overriding configuration with environment variables
This demonstrates how to override a specific configuration option ('configOption') for 'appname' using an environment variable. The environment variable takes precedence over the default value.
process.env.appname_configOption = 'newValue';
const config = require('rc')('appname', { configOption: 'defaultValue' });
Using command-line arguments to override configuration
This example shows how command-line arguments can be used to override the configuration in 'appname'. The command-line argument '--configOption newValue' overrides the default configuration.
// Run the application with: node app.js --configOption newValue
const config = require('rc')('appname', { configOption: 'defaultValue' });
Other packages similar to rc
config
Similar to rc, the 'config' package is used for managing configuration settings for Node.js applications. It supports loading configurations from files and environment variables. Compared to rc, 'config' provides a more structured approach to defining default configurations and environment-specific overrides.
dotenv
The 'dotenv' package is focused on loading environment variables from a .env file into process.env, providing a simple way to manage configuration settings. Unlike rc, dotenv does not support loading configurations from command-line arguments or merging configurations from multiple sources.
nconf
nconf is a hierarchical configuration management library for Node.js. It supports loading configuration from files, environment variables, command-line arguments, and even remote storage. nconf offers a more complex API compared to rc but provides greater flexibility in managing configurations from multiple sources.
rc
the non configurable configuration loader for lazy people.
Usage
the only option is to pass rc the name of your app, and your default configuration.
var rc = require('rc')(appname, {
//defaults go here.
port: 2468
})
Standards
Given your application name, rc will look in all the obvious places for configuration.
- command line arguments (parsed by optimist)
- enviroment variables prefixed with ${APPNAME}_
- if you passed an option
--config file
then from that file $HOME/.${APPNAME}rc
$HOME/.${APPNAME}/config
$HOME/.config/${APPNAME}
$HOME/.config/${APPNAME}/config
/etc/${APPNAME}rc
/etc/${APPNAME}/config
- the defaults object you passed in.
All configuration sources that where found will be flattened into one object,
so that sources earlier in this list override later ones.
Formats
Configuration files may be in either json
or ini
format.
Since ini, and env variables do not have a standard for types,
your application needs be prepared for strings.
License
BSD