Install and Start Using the Node.js SDK with Couchbase Server

The Couchbase Node.js SDK enables you to interact with a Couchbase Server cluster from the Node.js language.

The Couchbase Node.js SDK 3.0 is a complete rewrite of the API, reducing the number of overloads to present a simplified surface area, and adding support for future Couchbase Server features like Collections and Scopes (available in Couchbase Server 6.5 as a developer preview). The 3.0 SDK also brings in promises, to reduce the complexity of asynchronous javascript in client applications, as well as extending the management APIs and bringing better debugging and logging options

Installing the SDK

npm install couchbase

Information on new features, fixes, known issues, as well as information on how to install older release versions is in the release notes.

Hello Couchbase

Now you have the Node.js client installed, try out the following:

var cluster = new couchbase.Cluster(
    '', {
        username: 'Administrator',
        password: 'password'});

Couchbase uses Role Based Access Control (RBAC) to control access to resources. Here we will use the Full Admin role created during installation of the Couchbase Data Platform. For production client code, you will want to use more appropriate, restrictive settings, but here we want to get you up and running quickly. If you’re developing client code on the same VM or machine as the Couchbase Server, your URI can be localhost.

// get a bucket reference
var bucket = cluster.bucket('bucket-name');

If you installed the travel sample data bucket, substitute travel-sample for bucket-name.

// get a collection reference
var collection = bucket.defaultCollection();
var myCollection = bucket.collection("my-collection");

The latest release, 6.5, brings a limited Developer Preview of Collections, allowing Documents to be grouped by purpose or theme, according to a specified Collection. Here we will use the DefaultCollection, rather than a specific collection, but best practice would be to group your users into a single collection, groups into another, etc…​

// upsert document
var upsertResult = await collection.upsert("my-document", {name: 'mike'});

// get document
var getResult = await collection.get("my-document");

KV Operations are described in detail on the KV Operations page.

Additional Resources

The API reference is generated for each release and the latest can be found here.

Links to each release are to be found in the individual release notes.

The migrating-sdk-code-to-3.n.adoc highlights the main differences to be aware of when migrating your code.

Couchbase welcomes community contributions to the Node.js SDK. The Node.js SDK source code is available on GitHub.