@quantum-sec/node-config-utils
v1.1.0
Published
Utility for reading config values from files, command line arguments, and environment variables.
Downloads
4
Readme
node-config-utils
Utility for reading config values from files, command line arguments, and environment variables.
Installation
npm install --save @quantum-sec/node-config-utils
Usage
Create a reusable instance of ConfigService
passing a map of configuration options:
// example.config.ts
export const config = new ConfigService({
'my-option': {
description: 'An example configuration option',
required: true,
alias: 'o',
},
});
// usage.ts
import { config } from './example.config';
console.log(config['my-option']);
This will now populate the config
object with a key my-option
taking its value from a CLI argument,
environment variable (as MY_OPTION
), or from a YAML config file.
Precedence
When a value is specified in multiple mediums, the following order is observed when determining precedence (top of list takes highest precedence):
- CLI arguments
- Environment variables
- Configuration file
Environment Variable Prefix
You can optionally pass an environment variable prefix to the constructor. This is helpful when needing to avoid collisions.
const config = new ConfigService({ /* ... */ }, 'QS');
In the above example QS
would be prefixed to each environment variable name. For example, if you define
a config key my-option
which would normally search the environment variable MY_OPTION
, it would
instead search QS_MY_OPTION
.
Built-In Options
--help
(alias:-h
) – display the list of available command line arguments--version
(alias:-v
) – display the version of the application (from package.json)--config
(alias:-c
) – the path to the configuration file (default:./config.yaml
)