Skip to content

Manage a set of config variables that are dependent upon node environmental variables set on the server.

License

Notifications You must be signed in to change notification settings

racheldotey/node-config-service

Repository files navigation

node-config-service

Simple config config management for node apps.

npm GitHub all releases GitHub issues GitHub last commit

NPM

const nodeConfigService = require('node-config-service');

console.log(process.env);
Table of Contents

About

Keywords

NodeConfigService is a node module used to manage a set of config variables that are dependent upon node environmental variables set on the server.

Merge several configuration sources into a portable get method used throughout the app.

Getting Started

Prerequisites

Before starting, download and install Node.js. Node.js 14.0 or higher is required. Git is required to contribute to the project.

Optional tools include install yarn if desired. $ npm install --global yarn

Installation

NodeConfigService is a Node.js module available through the npm and yarn package registries.

If you have not already done so, create a package.json for your project with the npm init or the yarn init command.

Including this module using the npm install or the yarn install command:

In a terminal, type :

$ npm install node-config-service --save-dev
# or
$ yarn add node-config-service --dev

Features

  • Define config params in a json file.
  • Load config variables with node env params, pass them programmatically, or load them from asynchronous sources.

Usage

Process environmental variables can be set in a number of ways including the command line interface, in the package.json and using .env files in the root of your project.

Under the hood NodeConfigService uses Dotenv to populate the node process.env object with the defined variables.

require('node-config-service');

console.log(process.env);

Dotenv-expand adds variable expansion on top of dotenv . This allows the use of simple and complex examples of variable expansion in your .env file.

Example Env Variables

Example .env Variables
Basic Example
NODE_ENV=development
PROPERTY_KEY=property_value
Example DB Config
NODE_ENV=production

MONGOLAB_DATABASE=heroku_db
MONGOLAB_USER=username
MONGOLAB_PASSWORD=password
MONGOLAB_DOMAIN=abcd1234.mongolab.com
MONGOLAB_PORT=12345
MONGOLAB_URI=mongodb://${MONGOLAB_USER}:${MONGOLAB_PASSWORD}@${MONGOLAB_DOMAIN}:${MONGOLAB_PORT}/${MONGOLAB_DATABASE}
Full Dotenv Expanded Example
NODE_ENV=test
BASIC=basic

BASIC_EXPAND=$BASIC

MACHINE=machine_env
MACHINE_EXPAND=$MACHINE

UNDEFINED_EXPAND=$UNDEFINED_ENV_KEY

ESCAPED_EXPAND=\$ESCAPED

DEFINED_EXPAND_WITH_DEFAULT=${MACHINE:-default}
DEFINED_EXPAND_WITH_DEFAULT_NESTED=${MACHINE:-${UNDEFINED_ENV_KEY:-default}}

UNDEFINED_EXPAND_WITH_DEFINED_NESTED=${UNDEFINED_ENV_KEY:-${MACHINE:-default}}
UNDEFINED_EXPAND_WITH_DEFAULT=${UNDEFINED_ENV_KEY:-default}
UNDEFINED_EXPAND_WITH_DEFAULT_NESTED=${UNDEFINED_ENV_KEY:-${UNDEFINED_ENV_KEY_2:-default}}
UNDEFINED_EXPAND_WITH_DEFAULT_NESTED_TWICE=${UNDEFINED_ENV_KEY:-${UNDEFINED_ENV_KEY_2${UNDEFINED_ENV_KEY_3:-default}}}
UNDEFINED_EXPAND_WITH_DEFAULT_WITH_SPECIAL_CHARACTERS=${UNDEFINED_ENV_KEY:-/default/path}

MONGOLAB_DATABASE=heroku_db
MONGOLAB_USER=username
MONGOLAB_PASSWORD=password
MONGOLAB_DOMAIN=abcd1234.mongolab.com
MONGOLAB_PORT=12345
MONGOLAB_URI=mongodb://${MONGOLAB_USER}:${MONGOLAB_PASSWORD}@${MONGOLAB_DOMAIN}:${MONGOLAB_PORT}/${MONGOLAB_DATABASE}

MONGOLAB_USER_RECURSIVELY=${MONGOLAB_USER}:${MONGOLAB_PASSWORD}
MONGOLAB_URI_RECURSIVELY=mongodb://${MONGOLAB_USER_RECURSIVELY}@${MONGOLAB_DOMAIN}:${MONGOLAB_PORT}/${MONGOLAB_DATABASE}

WITHOUT_CURLY_BRACES_URI=mongodb://$MONGOLAB_USER:$MONGOLAB_PASSWORD@$MONGOLAB_DOMAIN:$MONGOLAB_PORT/$MONGOLAB_DATABASE
WITHOUT_CURLY_BRACES_USER_RECURSIVELY=$MONGOLAB_USER:$MONGOLAB_PASSWORD
WITHOUT_CURLY_BRACES_URI_RECURSIVELY=mongodb://$MONGOLAB_USER_RECURSIVELY@$MONGOLAB_DOMAIN:$MONGOLAB_PORT/$MONGOLAB_DATABASE
WITHOUT_CURLY_BRACES_UNDEFINED_EXPAND_WITH_DEFAULT_WITH_SPECIAL_CHARACTERS=$UNDEFINED_ENV_KEY:-/default/path

See dotenv-expand#examples for more information.

API Reference

NodeConfigService

NodeJS Config Service.

Simplified config management for node applications. NodeConfigService accepts a configuration object of properties to be defined using the node process env and run time overrides.

NodeConfigService.NodeConfigService#version

The NodeConfigService version

Example:

console.log(nodeConfigService.version); // '1.x.x'

Kind: static property of NodeConfigService Api: public Properties

Name
version

NodeConfigService.NodeConfigService#NodeConfigService

The NodeConfigService constructor

The exports of the nodeConfigService module is an instance of this class.

Example:

const appConfig = require('nodeConfigService');
const moduleConfig = new nodeConfigService.NodeConfigService();

Kind: static property of NodeConfigService Access: public Properties

Name
NodeConfigService

NodeConfigService.NodeConfigService#errors

Key and class object map of custom error methods.

Kind: static property of NodeConfigService Access: public Properties

Name Type Description
errors Object Key and class object map of custom error methods.

NodeConfigService.module.exports : NodeConfigService

The exports object is an instance of NodeConfigService.

Kind: static property of NodeConfigService Access: public

"loadEnv"

Load environment variables into process.env using dotenv.

NOTE: Should be placed as early as possible on startup.

Kind: event emitted by NodeConfigService

Issues

If you experience unexpected behavior please let the maintainers know.

To help us quickly find and solve your problem, pease try to create bug reports that are:

  • Reproducible. Include steps to reproduce the problem.
  • Specific. Include as much detail as possible: which version, what environment, etc.
  • Unique. Do not duplicate existing opened issues.
  • Scoped to a Single Bug. One bug per report.

Development

ForTheBadge built-by-developers

Dev requirements

Scripts

To view an interactive menu of project commands, first install the dependencies, then use execute the start command using either npm or yarn

View the interactive cli tool that lists and run package.json scripts by using:

$ npm install
$ npm start
# or
$ yarn install
$ yarn start
$ ntl
√  Node Task List
? Select a task to run: (Use arrow keys)
> start
  menu
  info
  test
  format
  build
  build-docs
(Move up and down to reveal more choices)

Running Tests

To run the test suite, first install the dependencies, then run the test command:

$ npm install
$ npm test
# or
$ yarn install
$ yarn test

Contributing

First off, thanks for taking the time to contribute! Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

GitHub forks GitHub last commit GitHub watchers

Contact

If you like my work, then give me a star or follow my GitHub.

License

Freely distributed under the MIT License. Copyright © Rachel Dotey, 2021-present.