Installing Airbrake in a JavaScript application

Features

  • Easy and flexible installation options including NPM, Bower and a public CDN
  • Send uncaught errors to Airbrake or manually using a try/catch
  • Add custom parameters to your errors for more context
  • Source map support
  • Control which errors you send with customizable filtering options
  • Works well with all frameworks
  • Support for the Backbone.js library

Installation

Visit our official GitHub repository for full installation instructions and configuration options.

The notifier is built using a standalone browserify build and can be used with:

We include the full source code with the package, so you can use Browserify too.

If you prefer not to host the library yourself, airbrake-js is available on the excellent cdnjs CDN.

Basic Usage

First you need to initialize notifier with project id and API key taken from Airbrake.io:

var airbrake = new airbrakeJs.Client({projectId: 1, projectKey: 'abc'});

Or if you are using browserify/webpack/etc:

var airbrakeJs = require('airbrake-js');
var airbrake = new airbrakeJs({projectId: 1, projectKey: 'abc'});

The simplest method is to report errors directly:

try {
  // This will throw if the document has no head tag
  document.head.insertBefore(document.createElement("style"));
} catch(err) {
  airbrake.notify(err);
  throw err;
}

Alternatively you can wrap any code which may throw errors using the client’s wrap method:

var startApp = function() {
  // This will throw if the document has no head tag.
  document.head.insertBefore(document.createElement("style"));
}
startApp = airbrake.wrap(startApp);

// Any exceptions thrown in startApp will be reported to Airbrake.
startApp();

Visit our official GitHub repository for more information.