@sentry/node package provides error tracking and performance monitoring for Node.js applications using OpenTelemetry for automatic instrumentation.
Prerequisites
- Node.js 18 or newer
- A Sentry account and project DSN
Installation
1
Install the Package
Install Current Version: 10.42.0
@sentry/node using your preferred package manager:2
Create Instrumentation File
Create a file named
instrument.js (or instrument.mjs for ESM) that initializes Sentry. This file must be imported before any other modules:3
Import Instrumentation in Your Application
Import the instrumentation file as the first import in your application:
4
ESM: Use --import Flag (Recommended)
For ESM applications, use Node.js’s Or set it via environment variable:
--import flag to ensure Sentry loads before your application:5
Verify Installation
Test that Sentry is working:Check your Sentry dashboard to see the error.
Automatic Instrumentation
The Node.js SDK uses OpenTelemetry to automatically instrument popular frameworks and libraries:- HTTP/HTTPS:
http,https,node-fetch,undici - Databases: PostgreSQL, MySQL, MySQL2, MongoDB, Mongoose, Redis, Prisma
- Frameworks: Express, Fastify, Koa, Hapi, Connect, NestJS
- GraphQL: GraphQL server instrumentation
- Message Queues: Kafka, AMQP
- Other: Knex, Tedious, Generic Pool, Dataloader
All automatic instrumentation happens when you call
Sentry.init() before importing other modules. No additional configuration is required.Framework Integrations
Express
Fastify
Koa
Hapi
Database Integrations
Prisma
MongoDB
PostgreSQL
Usage
Capturing Errors
Capturing Messages
Setting Context
Custom Spans
Create custom performance spans:Cron Monitoring
Monitor scheduled jobs:withMonitor helper:
Advanced Configuration
Environment and Release
Sampling
Filtering Events
Integrations
Troubleshooting
Missing Traces
If you’re not seeing traces:- Ensure
Sentry.init()is called before importing other modules - Check that
tracesSampleRateis set to a value greater than 0 - For ESM, use the
--importflag
Duplicate Errors
If you’re seeing duplicate errors, make sure you’re not:- Calling
Sentry.init()multiple times - Manually capturing errors that are automatically captured
- Using multiple error handlers that both report to Sentry
Source Maps
For TypeScript or transpiled code:-
Enable source maps in your
tsconfig.json: -
Use
Sentry.rewriteFramesIntegration()to map stack traces:
Next Steps
- Configure Performance Monitoring
- Set up Cron Monitoring
- Learn about Distributed Tracing
- Explore Custom Instrumentation