{"id":21743939,"url":"https://github.com/telefonica/node-merge-config","last_synced_at":"2025-04-13T05:06:39.175Z","repository":{"id":34137843,"uuid":"37972315","full_name":"Telefonica/node-merge-config","owner":"Telefonica","description":"Merge multiple configuration sources: JSON and YAML files, directories, environment properties and command-line arguments.","archived":false,"fork":false,"pushed_at":"2017-02-09T16:20:46.000Z","size":20,"stargazers_count":19,"open_issues_count":0,"forks_count":3,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-03-26T21:52:46.440Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Telefonica.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-24T08:30:07.000Z","updated_at":"2023-08-22T22:20:47.000Z","dependencies_parsed_at":"2022-08-17T23:10:19.117Z","dependency_job_id":null,"html_url":"https://github.com/Telefonica/node-merge-config","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Telefonica%2Fnode-merge-config","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Telefonica%2Fnode-merge-config/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Telefonica%2Fnode-merge-config/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Telefonica%2Fnode-merge-config/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Telefonica","download_url":"https://codeload.github.com/Telefonica/node-merge-config/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248272409,"owners_count":21075904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-26T07:09:28.378Z","updated_at":"2025-04-13T05:06:39.149Z","avatar_url":"https://github.com/Telefonica.png","language":"JavaScript","readme":"# merge-config\n\nMerge multiple configuration sources: configuration files (json and yaml), command-line arguments, and environment properties.\n\n[![npm version](https://badge.fury.io/js/merge-config.svg)](http://badge.fury.io/js/merge-config)\n[![Build Status](https://travis-ci.org/telefonica/node-merge-config.svg)](https://travis-ci.org/telefonica/node-merge-config)\n[![Coverage Status](https://img.shields.io/coveralls/telefonica/node-merge-config.svg)](https://coveralls.io/r/telefonica/node-merge-config)\n\nIt is inspired on [nconf](https://github.com/indexzero/nconf) library, offering a compatible API. However, there are some important differences:\n\n* The order of preference is reversed. For example, the default values have to be merged before the custom values so that custom values overwrite the default ones.\n* Support for multiple instances. It is possible to handle several configurations (e.g. to handle the configuration of several plugins independently).\n* Both **json** and **yaml** formats are supported when merging configuration files.\n* It is possible to merge a directory of configuration files, merging each configuration file alphabetically.\n* JSON files are parsed with [hjson](http://hjson.org/) to support comments.\n* The environment variable names are converted into camelCase when merging them into the configuration. The goal is to have a common notation with keys in JSON documents.\n\nThis module works with a single configuration object. This configuration object is updated with each merge (with preference for the last merge over the first one). The recursive merge is implemented with [lodash](https://lodash.com/docs#merge). This approach is useful to merge a default configuration with a custom configuration where only the configuration properties to be modified are included.\n\nFor example, with the following default configuration:\n\n```yml\nserverPort: 4070\nbaseLocationUrl: https://telefonica.com\nbasePath: /sample\nlogFormat: json\ndatabase:\n  uri: \"mongodb://localhost:27017/sample\"\n  options:\n    db:\n      bufferMaxEntries: 0\n  config:\n    defaultLimit: 100\n    maxLimit: 1000\n\n```\n\nTo only change the database URI, just merge the following configuration file:\n\n```yml\ndatabase:\n  uri: \"mongodb://mongodb-1:27017/sample\"\n```\n\n## Installation\n\n```bash\nnpm install merge-config\n```\n\n## Basic usage\n\n```js\nvar Config = require('merge-config');\n\n// Create a config instance\nvar config = new Config();\n\n// Add a default configuration file (at __dirname/config/config.json)\nconfig.file(path.join(__dirname, 'config', 'config.json'));\n// Add all the JSON files in config directory (added in alphabetically order)\nconfig.file(process.env.CONFIG_DIR);\n// Add a command-line argument.\nconfig.argv(['logFormat']);\n// Add an environment variable (LOG_LEVEL).\nconfig.env(['LOG_LEVEL']);\n\n// Get the whole merged configuration\nconsole.log(\"Merged configuration: %j\", config.get());\n// Get a single configuration property\nconsole.log(\"Log level: %s\", config.get('logLevel'));\n```\n\n## API\n\n### new Config(options)\n\nConstructor of a configuration instance.\n\n```js\nvar config = new Configuration();\n```\n\nIt is possible to specify the **delimiter** when using keys that refer to an element in the hierarchy of the configuration object. By default, the delimiter is `:` to keep compatibility with nconf. It is possible to change the delimiter:\n\n```js\nvar config = new Configuration({delimiter: '.'});\n```\n\n### get(path)\n\nRetrieve the configuration.\n\n```js\n// Get the whole merged configuration as an object\nconfig.get();\n```\n\nIt is possible to get a specific element in the configuration object:\n\n```js\n// Get the whole merged configuration as an object\nconfig.get('database:uri');\n```\n\n### set(path, value)\n\nSet a value (string, number, object, array).\n\n```js\n// Get the whole merged configuration as an object\nconfig.set('database:uri', 'mongodb://mongodb-1:27017/sample');\n```\n\n### merge(...sources)\n\nMerge a set of sources with the configuration object. These sources must be objects.\n\n```js\n// Get the whole merged configuration as an object\nconfig.merge({\n  database: {\n    uri: 'mongodb://mongodb-1:27017/sample'\n  }\n});\n```\n\n### env(whitelist)\n\nLoad the environment variables into the configuration.\n\n```js\n// Add all environment variables into merged configuration\nconfig.env();\n```\n\nIt is possible to restrict which environment variables are to be added to the configuration with optional parameter `whitelist` (array of strings).\n\n```js\n// Add only environment variables: LOG_LEVEL and PORT\nconfig.env(['LOG_LEVEL', 'PORT']);\n```\n\nThe keys from environment variables are transformed into camelCase to keep coherence with JSON format.\n\n### argv(whitelist)\n\nLoad the command-line arguments into the configuration.\n\n```js\n// Add all command-line arguments into merged configuration\nconfig.argv();\n```\n\nIt is possible to restrict which command-line arguments are to be added to the configuration with optional parameter `whitelist` (array of strings).\n\n```js\n// Add only environment variables: logLevel and port\nconfig.argv(['logLevel', 'port']);\n```\n\n### file(path)\n\nLoads a configuration file (if path targets a file), a set of configuration files located in a directory (if path targets a directory), or raises an error (if path is invalid).\n\n**NOTE**: If path targets a directory, the library merges any configuration file located in the directory, in alphabetical order, and without subdirectory recursion.\n\nIt support json and yaml configuration files which are identified by the file extension. JSON configuration files are parsed with [hjson](https://github.com/laktak/hjson-js) format to support comments in the JSON document.\n\n## License\n\nCopyright 2015 [Telefónica Investigación y Desarrollo, S.A.U](http://www.tid.es)\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelefonica%2Fnode-merge-config","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftelefonica%2Fnode-merge-config","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftelefonica%2Fnode-merge-config/lists"}