Contact

Technology

Dec 07, 2015

JSON Data Node Modules Part 1: Using NPM to Store and Distribute Static Data

Michael Gyarmathy

Michael Gyarmathy

Default image background

The following post is the first of a two-part series covering JSON data node modules and how to use them in the browser with Browserify. This post covers how to create JSON data node modules and potential use cases for this type of module.

NodeJS modules, along with Node Package Manager (NPM), are a great way for JavaScript developers to share modularized bits of code. By publishing your module to NPM, anyone can include it in their NodeJS application with a simple npm install your-package-name. NodeJS developers have also used local node modules to store configuration data used throughout their application. In this way, config data can be accessed by var config = require('path/to/config.json').

But there’s also a third, less-common usage: node modules can also be used for storing and distributing static data. I refer to these types of node modules as JSON data node modules. These modules are particularly useful when your application requires information that is public, static, and domain-specific. NPM can be leveraged to host and distribute static data. In addition, these modules are a great alternative to using a database to store and access your information.

There are several use cases that make this type of module suitable. A few examples are:

For example, say that you have a set of employee information that is shared among several different internal applications. This data is relatively static, only updated occasionally as employees join or leave the company.

// mycompany-employee-data.json [ { "id": 101, "img": "https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg", "name": "Sam Crawford", "department": "Marketing" }, { "id": 102, "img": "https://s3.amazonaws.com/uifaces/faces/twitter/uxceo/128.jpg", "name": "Jane Reed", "department": "HR" }, ... ]

Employee data is fictional and has been provided by uiFaces and uiNames.

The data above can be wrapped in a node module with just a single line of code:

// index.js module.exports = require('./mycompany-employee-data.json');

Publish your module to NPM using a descriptive name to make it publicly available to other NodeJS developers. (If your module’s data is private, consider using NPM private modules to control who has access to it.) In case you need to update the information stored in your module, be sure to update its version number so anyone who uses it can pull the updated information with npm update. For more information on how to create and publish a node module to NPM, see the official NPM documentation.

If you’d like to learn more about using JSON data node modules on the front-end, read my follow-up to this post: JSON Data Node Modules Part 2: In the Browser With Browserify.

References:

Conversation Icon

Contact Us

Ready to achieve your vision? We're here to help.

We'd love to start a conversation. Fill out the form and we'll connect you with the right person.

Searching for a new career?

View job openings