Gathering detailed insights and metrics for conf
Gathering detailed insights and metrics for conf
npm install conf
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (100%)
Total Downloads
147,520,908
Last Day
64,218
Last Week
395,281
Last Month
3,144,386
Last Year
42,666,749
1,247 Stars
190 Commits
127 Forks
12 Watching
1 Branches
48 Contributors
Latest Version
13.1.0
Package Id
conf@13.1.0
Unpacked Size
46.34 kB
Size
11.72 kB
File Count
7
NPM Version
10.9.0
Node Version
23.3.0
Publised On
07 Dec 2024
Cumulative downloads
Total Downloads
Last day
-13.6%
64,218
Compared to previous day
Last week
-43.9%
395,281
Compared to previous week
Last month
-34%
3,144,386
Compared to previous month
Last year
45.2%
42,666,749
Compared to previous year
Simple config handling for your app or module
All you have to care about is what to persist. This module will handle all the dull details like where and how.
It does not support multiple processes writing to the same store.
I initially made this tool to let command-line tools persist some data.
If you need this for Electron, check out electron-store
instead.
1npm install conf
1import Conf from 'conf'; 2 3const config = new Conf({projectName: 'foo'}); 4 5config.set('unicorn', '🦄'); 6console.log(config.get('unicorn')); 7//=> '🦄' 8 9// Use dot-notation to access nested properties 10config.set('foo.bar', true); 11console.log(config.get('foo')); 12//=> {bar: true} 13 14config.delete('unicorn'); 15console.log(config.get('unicorn')); 16//=> undefined
Changes are written to disk atomically, so if the process crashes during a write, it will not corrupt the existing config.
Returns a new instance.
Type: object
Type: object
Default values for the config items.
Note: The values in defaults
will overwrite the default
key in the schema
option.
Type: object
JSON Schema to validate your config data.
This will be the properties
object of the JSON schema. That is, define schema
as an object where each key is the name of your data's property and each value is a JSON schema used to validate that property.
Example:
1import Conf from 'conf'; 2 3const schema = { 4 foo: { 5 type: 'number', 6 maximum: 100, 7 minimum: 1, 8 default: 50 9 }, 10 bar: { 11 type: 'string', 12 format: 'url' 13 } 14}; 15 16const config = new Conf({ 17 projectName: 'foo', 18 schema 19}); 20 21console.log(config.get('foo')); 22//=> 50 23 24config.set('foo', '1'); 25// [Error: Config schema violation: `foo` should be number]
Note: The default
value will be overwritten by the defaults
option if set.
Type: object
Top-level properties for the schema, excluding properties
field.
Example:
1import Conf from 'conf'; 2 3const store = new Conf({ 4 projectName: 'foo', 5 schema: { /* … */ }, 6 rootSchema: { 7 additionalProperties: false 8 } 9});
Type: object
Under the hood, the JSON Schema validator ajv is used to validate your config. We use JSON Schema draft-2020-12 and support all validation keywords and formats.
Note: By default, allErrors
and useDefaults
are both set to true
, but can be overridden.
Example:
1import Conf from 'conf'; 2 3const store = new Conf({ 4 projectName: 'foo', 5 schema: { /* … */ }, 6 rootSchema: { 7 additionalProperties: false 8 }, 9 ajvOptions: { 10 removeAdditional: true 11 } 12});
Type: object
Important: I cannot provide support for this feature. It has some known bugs. I have no plans to work on it, but pull requests are welcome.
You can use migrations to perform operations to the store whenever a project version is upgraded.
The migrations
object should consist of a key-value pair of 'version': handler
. The version
can also be a semver range.
Example:
1import Conf from 'conf'; 2 3const store = new Conf({ 4 projectName: 'foo', 5 projectVersion: …, 6 migrations: { 7 '0.0.1': store => { 8 store.set('debugPhase', true); 9 }, 10 '1.0.0': store => { 11 store.delete('debugPhase'); 12 store.set('phase', '1.0.0'); 13 }, 14 '1.0.2': store => { 15 store.set('phase', '1.0.2'); 16 }, 17 '>=2.0.0': store => { 18 store.set('phase', '>=2.0.0'); 19 } 20 } 21});
Note: The version the migrations use refers to the project version by default. If you want to change this behavior, specify the
projectVersion
option.
Type: Function
Default: undefined
The given callback function will be called before each migration step.
The function receives the store as the first argument and a context object as the second argument with the following properties:
fromVersion
- The version the migration step is being migrated from.toVersion
- The version the migration step is being migrated to.finalVersion
- The final version after all the migrations are applied.versions
- All the versions with a migration step.This can be useful for logging purposes, preparing migration data, etc.
Example:
1import Conf from 'conf'; 2 3console.log = someLogger.log; 4 5const mainConfig = new Conf({ 6 projectName: 'foo1', 7 beforeEachMigration: (store, context) => { 8 console.log(`[main-config] migrate from ${context.fromVersion} → ${context.toVersion}`); 9 }, 10 migrations: { 11 '0.4.0': store => { 12 store.set('debugPhase', true); 13 }, 14 } 15}); 16 17const secondConfig = new Conf({ 18 projectName: 'foo2', 19 beforeEachMigration: (store, context) => { 20 console.log(`[second-config] migrate from ${context.fromVersion} → ${context.toVersion}`); 21 }, 22 migrations: { 23 '1.0.1': store => { 24 store.set('debugPhase', true); 25 }, 26 } 27});
Type: string
Default: 'config'
Name of the config file (without extension).
Useful if you need multiple config files for your app or module. For example, different config files between two major versions.
Type: string
Required unless you specify the cwd
option.
You can fetch the name
field from package.json.
Type: string
Required if you specify the migration
option.
You can fetch the version
field from package.json.
Type: string
Default: System default user config directory
You most likely don't need this. Please don't use it unless you really have to. By default, it will pick the optimal location by adhering to system conventions. You are very likely to get this wrong and annoy users.
Overrides projectName
.
The only use-case I can think of is having the config located in the app directory or on some external storage.
Type: string | Uint8Array | TypedArray | DataView
Default: undefined
Note that this is not intended for security purposes, since the encryption key would be easily found inside a plain-text Node.js app.
Its main use is for obscurity. If a user looks through the config directory and finds the config file, since it's just a JSON file, they may be tempted to modify it. By providing an encryption key, the file will be obfuscated, which should hopefully deter any users from doing so.
When specified, the store will be encrypted using the aes-256-cbc
encryption algorithm.
Type: string
Default: 'json'
Extension of the config file.
You would usually not need this, but could be useful if you want to interact with a file with a custom file extension that can be associated with your app. These might be simple save/export/preference files that are intended to be shareable or saved outside of the app.
Type: boolean
Default: false
The config is cleared if reading the config file causes a SyntaxError
. This is a good behavior for unimportant data, as the config file is not intended to be hand-edited, so it usually means the config is corrupt and there's nothing the user can do about it anyway. However, if you let the user edit the config file directly, mistakes might happen and it could be more useful to throw an error when the config is invalid instead of clearing.
Type: Function
Default: value => JSON.stringify(value, null, '\t')
Function to serialize the config object to a UTF-8 string when writing the config file.
You would usually not need this, but it could be useful if you want to use a format other than JSON.
Type: Function
Default: JSON.parse
Function to deserialize the config object from a UTF-8 string when reading the config file.
You would usually not need this, but it could be useful if you want to use a format other than JSON.
Type: string
Default: 'nodejs'
You most likely don't need this. Please don't use it unless you really have to.
Suffix appended to projectName
during config file creation to avoid name conflicts with native apps.
You can pass an empty string to remove the suffix.
For example, on macOS, the config file will be stored in the ~/Library/Preferences/foo-nodejs
directory, where foo
is the projectName
.
Type: boolean
Default: true
Accessing nested properties by dot notation. For example:
1import Conf from 'conf'; 2 3const config = new Conf({projectName: 'foo'}); 4 5config.set({ 6 foo: { 7 bar: { 8 foobar: '🦄' 9 } 10 } 11}); 12 13console.log(config.get('foo.bar.foobar')); 14//=> '🦄'
Alternatively, you can set this option to false
so the whole string would be treated as one key.
1import Conf from 'conf'; 2 3const config = new Conf({ 4 projectName: 'foo', 5 accessPropertiesByDotNotation: false 6}); 7 8config.set({ 9 `foo.bar.foobar`: '🦄' 10}); 11 12console.log(config.get('foo.bar.foobar')); 13//=> '🦄'
type: boolean
Default: false
Watch for any changes in the config file and call the callback for onDidChange
or onDidAnyChange
if set. This is useful if there are multiple processes changing the same config file.
Type: number
Default: 0o666
The mode that will be used for the config file.
You would usually not need this, but it could be useful if you want to restrict the permissions of the config file. Setting a permission such as 0o600
would result in a config file that can only be accessed by the user running the program.
Note that setting restrictive permissions can cause problems if different users need to read the file. A common problem is a user running your tool with and without sudo
and then not being able to access the config the second time.
You can use dot-notation in a key
to access nested properties.
The instance is iterable
so you can use it directly in a for…of
loop.
Set an item.
The value
must be JSON serializable. Trying to set the type undefined
, function
, or symbol
will result in a TypeError.
Set multiple items at once.
Get an item or defaultValue
if the item does not exist.
Reset items to their default values, as defined by the defaults
or schema
option.
Use .clear()
to reset all items.
Check if an item exists.
Delete an item.
Delete all items.
This resets known items to their default values, if defined by the defaults
or schema
option.
callback
: (newValue, oldValue) => {}
Watches the given key
, calling callback
on any changes.
When a key is first set oldValue
will be undefined
, and when a key is deleted newValue
will be undefined
.
Returns a function which you can use to unsubscribe:
1const unsubscribe = conf.onDidChange(key, callback); 2 3unsubscribe();
callback
: (newValue, oldValue) => {}
Watches the whole config object, calling callback
on any changes.
oldValue
and newValue
will be the config object before and after the change, respectively. You must compare oldValue
to newValue
to find out what changed.
Returns a function which you can use to unsubscribe:
1const unsubscribe = conf.onDidAnyChange(callback); 2 3unsubscribe();
Get the item count.
Get all the config as an object or replace the current config with an object:
1conf.store = { 2 hello: 'world' 3};
Get the path to the config file.
configstore
?I'm also the author of configstore
. While it's pretty good, I did make some mistakes early on that are hard to change at this point. This module is the result of everything I learned from making configstore
. Mainly where the config is stored. In configstore
, the config is stored in ~/.config
(which is mainly a Linux convention) on all systems, while conf
stores config in the system default user config directory. The ~/.config
directory, it turns out, often have an incorrect permission on macOS and Windows, which has caused a lot of grief for users.
The serialize
and deserialize
options can be used to customize the format of the config file, as long as the representation is compatible with utf8
encoding.
Example using YAML:
1import Conf from 'conf';
2import yaml from 'js-yaml';
3
4const config = new Conf({
5 projectName: 'foo',
6 fileExtension: 'yaml',
7 serialize: yaml.safeDump,
8 deserialize: yaml.safeLoad
9});
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
security policy file detected
Details
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 10/30 approved changesets -- score normalized to 3
Reason
4 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-06
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More