Installing Airbrake in a Node.js app

node flag

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

Installation

Using npm

npm install @airbrake/node

Using Yarn

yarn add @airbrake/node

Configuration

This is a simple Node app app.js file that throws an error and sends it to Airbrake. To configure Airbrake in your project, just require the @airbrake/node library and instantiate the notifier as shown:

const http = require('http');
const Airbrake = require('@airbrake/node');

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

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((_req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
  throw new Error('I am an uncaught exception');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

For our full Node example app code and more information, please visit our official GitHub repo.