Node.js SDK
For Node.js applications, including Express, Fastify, Koa, and other frameworks.
Installation
npm install @sentry/node
Setup
Initialize at the very top of your entry file, before importing anything else:
import * as Sentry from '@sentry/node';
Sentry.init({
dsn: 'https://your-key@your-hostname.tindra.sh/1',
environment: process.env.NODE_ENV,
release: process.env.npm_package_version,
tracesSampleRate: 0.1,
});
The order matters. Sentry must be initialized before other imports to instrument them correctly.
Express
import express from 'express';
import * as Sentry from '@sentry/node';
Sentry.init({ dsn: '...' });
const app = express();
// Request handler must be the first middleware
app.use(Sentry.Handlers.requestHandler());
// Your routes
app.get('/', (req, res) => {
res.send('Hello');
});
// Error handler must be before other error handlers
app.use(Sentry.Handlers.errorHandler());
Manual error capturing
try {
await processOrder(order);
} catch (error) {
Sentry.captureException(error);
throw error;
}
User context
Sentry.setUser({
id: req.user.id,
email: req.user.email,
});
Set this in a middleware that runs after authentication.
Custom spans
const span = Sentry.startSpan({ op: 'db.query', name: 'Fetch user orders' }, async () => {
return db.query('SELECT * FROM orders WHERE user_id = ?', [userId]);
});
Performance monitoring
Automatic instrumentation covers:
- HTTP request/response timing
- Database queries (Postgres via
pg, MySQL viamysql2, etc.) - Redis commands (
ioredis,redis) - Outbound HTTP requests (
http,https,fetch)
Testing
Sentry.captureException(new Error('Hello Tindra'));
Check your dashboard. The event should appear within a few seconds.