Node.js

raven-node is the official Node.js client for Sentry.

Note: If you’re using JavaScript in the browser, you’ll need raven-js.

Installation

Raven is distributed via npm:

$ npm install raven --save

Configuring the Client

Next you need to initialize the Raven client and configure it to use your Sentry DSN:

var Raven = require('raven');
Raven.config('___DSN___').install();

At this point, Raven is set up to capture and report any uncaught exceptions.

You can optionally pass an object of configuration options as the 2nd argument to Raven.config. For more information, see Configuration.

Reporting Errors

Raven’s install method sets up a global handler to automatically capture any uncaught exceptions. You can also report errors manually with try...catch and a call to captureException:

try {
    doSomething(a[0]);
} catch (e) {
    Raven.captureException(e);
}

You can also use wrap and context to have Raven wrap a function and automatically capture any exceptions it throws:

Raven.context(function () {
  doSomething(a[0]);
});

For more information on reporting errors, see Usage.

Adding Context

Code run via wrap or context has an associated set of context data, and Raven provides methods for managing that data.

You’ll most commonly use this to associate the current user with an exception:

Raven.context(function () {
  Raven.setContext({
    user: {
      email: 'matt@example.com',
      id: '123'
    }
  });
  // errors thrown here will be associated with matt
});
// errors thrown here will not be associated with matt

This can also be used to set tags and extra keys for associated tags and extra data.

You can update the context data with mergeContext or retrieve it with getContext. When an exception is captured by a wrapper, the current context state will be passed as options to captureException.

See context/wrap for more.

Middleware and Integrations

If you’re using Node.js with a web server framework/library like Connect, Express, or Koa, it is recommended to configure one of Raven’s server middleware integrations. See Integrations.

Deep Dive

For more detailed information about how to get most out of Raven there is additional documentation available that covers all the rest:

Resources: