Installing Airbrake in an Express application

Features

  • Easy and flexible installation options including npm and Yarn
  • Send uncaught errors to Airbrake or manually using a try/catch
  • Add custom parameters to your errors for more context
  • Private sourcemap support
  • Control which errors you send with customizable filtering options

Using Airbrake with Express.js

This example Node.js application uses Express.js and sets up Airbrake to report errors and performance data. To adapt this example to your app, follow these steps:

1. Install the package

npm install @airbrake/node

2. Include @airbrake/node and the Express.js instrumentation in your app

Include the required Airbrake libraries in your app.js

const Airbrake = require('@airbrake/node');
const airbrakeExpress = require('@airbrake/node/dist/instrumentation/express');

3. Configure Airbrake with your project’s credentials

const airbrake = new Airbrake.Notifier({
  projectId: process.env.AIRBRAKE_PROJECT_ID,
  projectKey: process.env.AIRBRAKE_PROJECT_KEY,
});

4. Add the Airbrake Express middleware

This middleware should be added before any routes are defined.

app.use(airbrakeExpress.makeMiddleware(airbrake));

5. Add the Airbrake Express error handler

The error handler middleware should be defined last. For more info on how this works, see the official Express error handling doc.

app.use(airbrakeExpress.makeErrorHandler(airbrake));

6. Run your app

The last step is to run your app. To test that you’ve configured Airbrake correctly, you can throw an error inside any of your routes:

app.get('/hello/:name', function hello(_req, _res) {
  throw new Error('hello from Express');
});

Any unhandled errors that are thrown will now be reported to Airbrake. See the basic usage to learn how to manually send errors to Airbrake and other advanced options.

To see this all in action, check out the full app.js file in our example on GitHub.