{"id":12987,"library":"confidence","title":"Confidence Configuration API","description":"Confidence is a configuration management library that provides a structured document format and an API for serving dynamic configuration values. It builds upon standard JSON objects, introducing special `$`-prefixed keys for filtering values based on defined criteria, facilitating environment-specific configurations, A/B testing, and dynamic value selection. The current stable version is 5.0.1, which requires Node.js v12 or higher; older Node.js environments (pre-v12) must use version 4.x. The project is actively maintained, with releases primarily driven by bug fixes, feature enhancements, and Node.js compatibility updates. Its key differentiators include built-in support for environment variables, criteria-based filtering, value ranges for A/B testing, and metadata attachment, making it a robust solution for complex application configurations.","status":"active","version":"5.0.1","language":"javascript","source_language":"en","source_url":"git://github.com/hapipal/confidence","tags":["javascript","hapi","plugin","configuration","api"],"install":[{"cmd":"npm install confidence","lang":"bash","label":"npm"},{"cmd":"yarn add confidence","lang":"bash","label":"yarn"},{"cmd":"pnpm add confidence","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Confidence v5+ is ESM-only. For CommonJS, use v4 or earlier, or a bundler that handles ESM to CJS conversion.","wrong":"const Store = require('confidence');","symbol":"Store","correct":"import { Store } from 'confidence';"},{"note":"The class is named 'Store', not 'ConfidenceStore', despite being referred to as 'confidenceStore' in some documentation snippets (e.g., 'new confidenceStore').","symbol":"ConfidenceStore","correct":"import { Store } from 'confidence';"}],"quickstart":{"code":"import { Store } from 'confidence';\n\nconst configDoc = {\n  \"key1\": \"abc\",\n  \"key2\": {\n    \"$filter\": \"env\",\n    \"production\": {\n      \"deeper\": \"production_value\"\n    },\n    \"$default\": {\n      \"$filter\": \"platform\",\n      \"android\": 0,\n      \"ios\": 1,\n      \"$default\": 2\n    }\n  },\n  \"key3\": {\n    \"sub1\": 123,\n    \"sub2\": {\n      \"$filter\": \"xfactor\",\n      \"yes\": 6,\n      \"$default\": 7\n    }\n  },\n  \"mysql\": {\n    \"host\": { \"$env\": \"MYSQL_HOST\" },\n    \"port\": { \"$env\": \"MYSQL_PORT\" },\n    \"user\": { \"$env\": \"MYSQL_USER\" }\n  }\n};\n\nconst store = new Store(configDoc);\n\n// Simulate environment variables for demonstration\nprocess.env.MYSQL_HOST = process.env.MYSQL_HOST ?? 'localhost';\nprocess.env.MYSQL_PORT = process.env.MYSQL_PORT ?? '3306';\nprocess.env.MYSQL_USER = process.env.MYSQL_USER ?? 'testuser';\n\nconsole.log('Without criteria:');\nconsole.log(store.get('/'));\n\nconst criteria = {\n  \"env\": \"production\",\n  \"platform\": \"ios\",\n  \"xfactor\": \"yes\"\n};\n\nconsole.log('\\nWith criteria:');\nconsole.log(store.get('/', criteria));\n\nconsole.log('\\nSpecific key with criteria:');\nconsole.log(store.get('/key2/deeper', criteria));\n\nconsole.log('\\nEnvironment variable based config:');\nconsole.log(store.get('/mysql'));","lang":"javascript","description":"Demonstrates initializing a Confidence Store, retrieving configuration values with and without criteria, and showcases environment variable integration."},"warnings":[{"fix":"Upgrade Node.js to v12 or higher, or explicitly install `confidence@4` for older Node.js environments (`npm install confidence@4`).","message":"Confidence v5.x drops support for Node.js versions older than v12. Applications running on Node.js v10 or earlier must remain on Confidence v4.x or upgrade their Node.js environment.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Update import statements to use ES Modules syntax (`import { Store } from 'confidence';`). For CJS-only projects, consider using dynamic `import()` or stick to `confidence@4`.","message":"Confidence v5.x is distributed as an ES Module (ESM) and no longer provides a CommonJS (CJS) entry point. Direct `require()` statements will fail in Node.js environments unless transpiled or using dynamic `import()`.","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Avoid using `$` as a prefix for your own configuration data keys. Restrict custom keys to alphanumeric characters and underscores.","message":"The configuration document uses special `$`-prefixed keys (e.g., `$filter`, `$default`, `$env`) for directives. Attempting to use these prefixes for regular data keys will lead to unexpected behavior or errors as they are reserved for Confidence's internal logic.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change `const { Store } = require('confidence');` to `import { Store } from 'confidence';` and ensure your project is configured for ESM (e.g., `\"type\": \"module\"` in `package.json`). For CJS-only projects, install `confidence@4`.","cause":"Attempting to `require('confidence')` in a CommonJS module when using Confidence v5+.","error":"ERR_REQUIRE_ESM"},{"fix":"Ensure `new Store(document)` is called with a valid JSON-like object before attempting to access methods like `get` or `meta`. Check the `document` for syntax errors.","cause":"Trying to call `store.get()` before initializing the `Store` instance with a configuration document, or passing an invalid document.","error":"TypeError: Cannot read properties of undefined (reading 'get')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}