npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

json-settings

v1.2.0

Published

Basic file-based settings manager that allows you to flush to a file.

Readme

#Json-Settings Settings was something I threw together quickly for a project I'm working on. I was unable to quickly find anything that was file-based and allowed for auto flushing/reloading. The key word being quickly, there may be other projects out there much better than this one. But for now, this one works for my needs.

#Usage

var settings = require("json-settings");
var main = new settings("./settings.json");
main.set("oauth", "s02m569hrro2gxxt65w5f5qa2x07d6");
main.get("oauth"); //s02m569hrro2gxxt65w5f5qa2x07d6

#API ####Constructor(string, object) The constructor's signature is function(string, object). The string is the relative path to the json file. The object is an options object, which consists of the following defaults:

  • skipReload (boolean; default: false)
    • Settings won't be reloaded from the constructor. You'll have to reload them manually.
  • reloadSync (boolean; default: true)
    • Settings will call reloadSync from the constructor (instead of regular reload)
  • cb (function)
    • The callback for the async reload call if reloadSync and skipReload are both false.
var settings = require("json-settings");
var main = new settings("./settings.json");

####reload(callback) Reload is asynchronous. It uses fs.readFile to grab the contents of the settings file and run JSON.parse on it. It then stores the parsed json in the raw_data object.

Reload's signature is function(callback) where the callback has the signature of function(error).

var settings = require("json-settings");
var main = new settings("./settings.json");
main.reload(function(err) {
  if (!err) {
    console.error(err);
  } else {
    console.log("Settings reloaded");
  }
});

####reloadSync() reloadSync is a synchronous version of reload. No errors are thrown, if there's an exception we reset the raw_data and swallow the exception.

var settings = require("json-settings");
var main = new settings("./settings.json");
main.reloadSync();
console.log("Reloaded!");

####save(callback) save will flush the json encoded raw_data object to the file.

var settings = require("json-settings");
var main = new settings("./settings.json");
main.save(function(err) {
  if (!err) {
    console.log("Settings saved!");
  } else {
    console.error(err);
  }
});

####saveSync() saveSync is the synchronous version of save

var settings = require("json-settings");
var main = new settings("./settings.json");
settings.saveSync();

####get(string, string) get's signature is get(setting_name, default_value)

get will return the value associated with the name provided.

var settings = require("json-settings");
var main = new settings("./settings.json");
console.log(main.get("oauth")); //assuming oauth exists and is set to 's02m569hrro2gxxt65w5f5qa2x07d6', 's02m569hrro2gxxt65w5f5qa2x07d6' will be printed to the console.

####set(string, mixed, object) set's signature is: set(setting_name, setting_value, options)

The options are as follows:

  • saveSync (boolean; default: true)
    • Will save synchronously if true
  • save (boolean; default: false)
    • If saveSync is false and this is true, we'll save asynchronously
  • cb (function)
    • If saveSync is false and save is true, we'll set this function as the callback for save

####setAll(object, object) setAll's signature is: setAll(object_containing_keys_and_values, options

The object_containing_keys_and_values is something along the lines of

{
    oauth: "abcdefghijklmnop",
    secret: "qrstuvwxyz"
}

The options here are the same from set above

var settings = require("json-settings");
var main = new settings("./settings.json");
main.setAll({
    oauth: "abcdefghijklmnop",
    secret: "qrstuvwxyz"
});

This will set oauth to abcdefghijklmnop and secret to qrstuvwxyz in the flatfile and save according to your options.

####getAll(array) getAll's signature is: getAll(array_of_keys_to_get) or getAll(array_of_objects_to_get)

The object array looks like:

[{
    key: "setting_name",
    defaultValue: "default setting value"
}]
var settings = require("json-settings");
var main = new settings("./settings.json");
console.log(main.getAll(["oauth", "secret", "non_existant_key"])); //{oauth: "abcdefghijklmnop", secret: "qrstuvwxyz", non_existant_key: undefined}