Socket
Socket
Sign inDemoInstall

configstore

Package Overview
Dependencies
12
Maintainers
9
Versions
34
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    configstore

Easily load and save config without having to think about where and how


Version published
Weekly downloads
8.4M
increased by0.63%
Maintainers
9
Install size
265 kB
Created
Weekly downloads
 

Package description

What is configstore?

The configstore npm package is used for easily loading and persisting config without having to think about where and how. It's ideal for storing user settings, application configurations, and other data that needs to be saved between sessions.

What are configstore's main functionalities?

Creating and managing a config store

This code demonstrates how to create a new config store associated with a specific package and set default values. It also shows how to retrieve a value from the store.

const Configstore = require('configstore');
const packageJson = require('./package.json');
const conf = new Configstore(packageJson.name, {foo: 'bar'});
console.log(conf.get('foo'));
//=> 'bar'

Setting and getting data

This code shows how to set and get data in the config store. It also demonstrates the use of dot-notation to work with nested properties.

conf.set('awesome', true);
console.log(conf.get('awesome'));
//=> true

// Using dot-notation for nested properties
conf.set('bar.baz', true);
console.log(conf.get('bar'));
//=> { baz: true }

Deleting data

This code snippet illustrates how to delete a key-value pair from the config store.

conf.delete('awesome');
console.log(conf.get('awesome'));
//=> undefined

Accessing the entire store

This code example shows how to access the entire config store, which returns an object containing all the key-value pairs.

console.log(conf.all);
//=> { foo: 'bar', bar: { baz: true } }

Checking if a key exists

This code demonstrates how to check if a particular key exists in the config store.

console.log(conf.has('foo'));
//=> true

Other packages similar to configstore

Readme

Source

configstore

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 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.
config.set('bar.baz', true);
console.log(config.get('bar'));
//=> {baz: true}

config.delete('awesome');
console.log(config.get('awesome'));
//=> undefined

API

Configstore(packageName, defaults?, options?)

Returns a new instance.

packageName

Type: string

Name of your package.

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 packageName and globalConfigPath options.

Instance

You can use dot-notation in a key to access nested properties.

.set(key, value)

Set an item.

.set(object)

Set multiple items at once.

.get(key)

Get an item.

.has(key)

Check if an item exists.

.delete(key)

Delete an item.

.clear()

Delete all items.

.size

Get the item count.

.path

Get the path to the config file. Can be used to show the user where the config file is located or even better open it for them.

.all

Get all the config as an object or replace the current config with an object:

config.all = {
	hello: 'world'
};

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

Keywords

FAQs

Last updated on 05 Apr 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc