JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 17
  • Score
    100M100P100Q65292F
  • License MIT

Use a JSON encoded file to automatically save a JavaScript value to disk whenever that value changes.

Package Exports

  • filejson

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (filejson) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

filejson

NPM

Use a JSON encoded file to automatically save a JavaScript value to disk whenever that value changes. A value can be a Javascript: string, number, boolean, null, object, or an array. The value can be structured in an array or an object to allow for more complex data stores. These structures can also be nested. As a result, you can use this module as a simple document store for storing semi structured data.

Requirements

ECMAScript 6 Reflect and Proxy objects support, which is found natively in Node.js >= 6. If you are using a version of Node.js < 6, use a polyfill, such as harmony-reflect. Proxy support is key for this module working so eloquently. Other non-Proxy based modules require function calls each time you wish to save an object. Unlike those, filejson is as easy as file.contents = "my new value" or file.contents = {"msg": "Hello World"} and the changes are automatically saved to disk.

Installation

npm install filejson --save

Additional installation and usage steps for those using Node.js 5 or earlier

npm install harmony-reflect --save
  • In addition to requiring filejson, you will need to require harmony-reflect at the top of your app, like this:
var Reflect = require('harmony-reflect');
  • Lastly, every time you run your app you will need to use the node --harmony_proxies flag, like this:
node --harmony_proxies index.js

Example usage

var Filejson = require("filejson");
var file1 = new Filejson();

file1.load("file1.json", proceed); // file1.json contains {"abc": "123"}

function proceed(error, file) {
    if(error) {
        console.error(error);
        return;
    }

    console.log(file.contents); // outputs {"abc": "123"}

    file.contents.msg = "Hello World"; // saves {"abc": "123", "msg": "Hello World"} to file1.json.

    console.log(file.contents); // outputs {"abc": "123", "msg": "Hello World"}

}