Easily load and persist config without having to think about where and how
The config is stored in a JSON file located in $XDG_CONFIG_HOME or ~/.config.
Example: ~/.config/configstore/some-id.json
If you need this for Electron, check out electron-store instead. And check out conf for a more modern version of configstore.
Install
npm install configstore
Usage
import fs from 'node:fs';import Configstore from 'configstore';const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));// Create a Configstore instance.const config = new Configstore(packageJson.name, {foo: 'bar'});console.log(config.get('foo'));//=> 'bar'config.set('awesome', true);console.log(config.get('awesome'));//=> true// Use dot-notation to access nested properties.
API
Configstore(id, defaults?, options?)
Returns a new instance.
id
Type: string
Identifier for your config. Usually your package name.
defaults
Type: object
Default config.
options
Type: object
globalConfigPath
Type: boolean
Default: false
Store the config at $CONFIG/package-name/config.json instead of the default $CONFIG/configstore/package-name.json. This is not recommended as you might end up conflicting with other tools, rendering the "without having to think" idea moot.
configPath
Type: string
Default: Automatic
Please don't use this option unless absolutely necessary and you know what you're doing.
Set the path of the config file. Overrides the id and globalConfigPath options.
clearInvalidConfig
Type: boolean
Default: true
Clear the config file if it contains invalid JSON. If set to false, a SyntaxError will be thrown instead of clearing the file. This allows you to recover corrupted config files manually.
Instance
You can use dot-notation in a key to access nested properties.
.set(key, value)
Set an item.
You can use dot-notation in a key to access nested properties.
.set(object)
Set multiple items at once.
.get(key)
Get an item.
You can use dot-notation in a key to access nested properties.