Getting Started with NodeJS

If you haven’t set up a Riak Node and started it, please visit Running A Cluster first.

To try this flavor of Riak, a working installation of Node.js 0.12 or later is required.

Code for these examples is available here. To run, follow these directions:

git clone git://
cd riak-nodejs-client-examples
npm install
node ./app.js

Client Setup

Install the Riak Node.js Client through NPM.

Connecting to Riak

Connecting to Riak with the Riak Node.js Client requires creating a new client object and using the callback argument to know when the client is fully initialized:

var Riak = require('basho-riak-client');
var nodes = [
var client = new Riak.Client(nodes, function (err, c) {
    // NB: at this point the client is fully initialized, and
    // 'client' and 'c' are the same object

This creates a new Riak.Client object which handles all the details of tracking active nodes and also provides load balancing. The Riak.Client object is used to send commands to Riak. When your application is completely done with Riak communications, the following method can be used to gracefully shut the client down and exit Node.js:

client.stop(function (err, rslt) {
    // NB: you may wish to check err

Let’s make sure the cluster is online with a Ping request:

var assert = require('assert'); (err, rslt) {
    if (err) {
        throw new Error(err);
    } else {
        // On success, ping returns true
        assert(rslt === true);

This is some simple code to test that a node in a Riak cluster is online - we send a simple ping message. Even if the cluster isn’t present, the Riak Node.js Client will return a response message. In the callback it is important to check that your activity was successful by checking the err variable.

We are now ready to start interacting with Riak.

Next Steps

CRUD Operations