Docs · Node.js

SDKs

Node.js

Bugzor works with the open-source @sentry/node SDK. Tested with version 11.12 on Node 22.

Install#

Terminal
npm install @sentry/node

Configure#

@sentry/node has to load before anything else, so it can instrument http and your framework. Put the setup in its own file:

instrument.mjs
import * as Sentry from "@sentry/node";

Sentry.init({
  dsn: process.env.SENTRY_DSN, // https://<public-key>@www.bugzor.com/<project-id>
  environment: process.env.NODE_ENV || "development",
  release: process.env.GIT_SHA,
  // Include the user's IP and request data (needed for "users affected").
  dataCollection: { userInfo: true },
});

Then load it first. With ES modules, use --import:

Terminal
node --import ./instrument.mjs app.mjs

With CommonJS, require("./instrument.js") on the first line of your entry file.

Express#

app.mjs
import express from "express";
import * as Sentry from "@sentry/node";

const app = express();

app.get("/", (req, res) => res.send("ok"));
app.get("/boom", () => {
  throw new Error("Something broke");
});

// After all routes, before your own error handlers.
Sentry.setupExpressErrorHandler(app);

app.listen(3000);

Fastify, Koa, Hapi and Connect have the same kind of helper: Sentry.setupFastifyErrorHandler(app) and so on.

Capture errors and messages#

JavaScript
try {
  JSON.parse(body);
} catch (cause) {
  Sentry.captureException(new Error("order import failed", { cause }));
}

Sentry.captureMessage("Payment provider responded slowly", "warning");

Errors with a cause show the whole chain.

Add context#

JavaScript
Sentry.setUser({ id: user.id, email: user.email });

Sentry.withScope((scope) => {
  scope.setTags({ component: "checkout" });
  scope.setContext("cart", { items: 3, total_cents: 4599 });
  Sentry.captureMessage("Cart total mismatch");
});

Scripts and serverless#

Events are sent asynchronously. In a script or a function that exits right away, wait for them:

JavaScript
await Sentry.flush(2000);

Test it#

Terminal
SENTRY_DSN="https://<public-key>@www.bugzor.com/<project-id>" node --input-type=module -e '
import * as Sentry from "@sentry/node";
Sentry.init({ dsn: process.env.SENTRY_DSN });
Sentry.captureMessage("Hello from Node");
await Sentry.flush(2000);'

Add debug: true to init to see every request in the console.

Something unclear or out of date? Tell us.