Node.js
For Express, Fastify, Koa, Nest, Hapi, background workers and scripts, use the official @sentry/node
SDK. For code that runs in the browser, follow the JavaScript guide instead.
1. Install the SDK
npm install @sentry/node2. Initialize it before anything else
Put the setup in a file of its own, so it can load before your app's other modules:
const Sentry = require("@sentry/node")
Sentry.init({
dsn: process.env.SENTRY_DSN, // https://<key>@uncaught.thistinct.tools/<project-id>
environment: process.env.NODE_ENV,
release: process.env.GIT_SHA,
tracesSampleRate: 0.1, // time one in ten requests for Performance
includeLocalVariables: true // show each frame's local variables
})Require it on the first line of your entry file, or preload it for ES modules:
node --import ./instrument.mjs server.mjs3. Catch exceptions
Uncaught exceptions and unhandled promise rejections are reported on their own. For Express, add the error handler after all your routes, and before any error handler of your own:
require("./instrument")
const Sentry = require("@sentry/node")
const express = require("express")
const app = express()
app.get("/orders/:id", showOrder)
Sentry.setupExpressErrorHandler(app)
app.listen(3000)Fastify, Koa and Hapi have setupFastifyErrorHandler, setupKoaErrorHandler and setupHapiErrorHandler. Anywhere else, report what you catch:
try {
await syncInventory()
} catch (error) {
Sentry.captureException(error)
throw error
}Add context
Sentry.setUser({ id: user.id, email: user.email })
Sentry.setTag("queue", "imports")TypeScript and bundled code
If you ship compiled or bundled code, upload its source maps with sentry-cli as described in the
releases guide, and stack traces point at your original .ts files.