npm install @airbrake/node
Include the required Airbrake libraries in your app.js
const Airbrake = require('@airbrake/node');
const airbrake = new Airbrake.Notifier({
projectId: process.env.AIRBRAKE_PROJECT_ID,
projectKey: process.env.AIRBRAKE_PROJECT_KEY,
});
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:
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}/`);
});
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.
Note: to see this all in action, take a look at our
example app.js
file
and to run the example, follow the next steps.
If you want to run this example application locally, follow these steps:
git clone [email protected]:airbrake/airbrake-js.git
cd airbrake-js/packages/node/examples/nodejs
npm install
AIRBRAKE_PROJECT_ID=your-id AIRBRAKE_PROJECT_KEY=your-key node app.js
firefox localhost:3000