Skip to main content
The @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 @sentry/node using your preferred package manager:
Current Version: 10.42.0
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:
You must call Sentry.init() before requiring/importing any other modules. This ensures that Sentry can automatically instrument all modules in your application.
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 --import flag to ensure Sentry loads before your application:
Or set it via environment variable:
Using --import ensures that Sentry is initialized before any application code runs, providing the most complete instrumentation.
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:
Or use the withMonitor helper:

Advanced Configuration

Environment and Release

Sampling

Filtering Events

Integrations

Troubleshooting

Missing Traces

If you’re not seeing traces:
  1. Ensure Sentry.init() is called before importing other modules
  2. Check that tracesSampleRate is set to a value greater than 0
  3. For ESM, use the --import flag

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:
  1. Enable source maps in your tsconfig.json:
  2. Use Sentry.rewriteFramesIntegration() to map stack traces:
Remember to flush events before your application exits, especially in serverless environments:

Next Steps