Skip to main content
The @sentry/nextjs package provides comprehensive error tracking and performance monitoring for Next.js applications, with automatic instrumentation for both client and server components.

Prerequisites

  • Node.js 18 or newer
  • Next.js 13.2.0 or newer (supports Next.js 13, 14, 15, and 16)
  • A Sentry account and project DSN

Quick Installation with Wizard

The fastest way to get started is using the Sentry Next.js Wizard:
The wizard will:
  1. Install @sentry/nextjs
  2. Create configuration files
  3. Set up source map uploads
  4. Add example error buttons (optional)
The wizard automates the entire setup process. If you prefer manual installation, follow the steps below.

Manual Installation

1

Install the Package

Install @sentry/nextjs using your preferred package manager:
Current Version: 10.42.0
2

Create Configuration Files

Create three configuration files in your project root:sentry.client.config.ts - Client-side configuration:
sentry.server.config.ts - Server-side configuration:
sentry.edge.config.ts - Edge runtime configuration:
3

Configure Next.js

Wrap your Next.js configuration with Sentry in next.config.js or next.config.mjs:
4

Set Environment Variables

Add your Sentry auth token to .env.local:
Never commit your .env.local file to version control. Add it to .gitignore.
5

Verify Installation

Create a test page to verify Sentry is working:
Visit the page and click the button. Check your Sentry dashboard to see the error.

App Router (Next.js 13+)

Server Components

For Server Components, use Sentry in Server Actions:

Client Components

For Client Components:

Error Boundary

Use Sentry’s Error Boundary:

API Routes

Wrap API routes with Sentry:

Pages Router (Legacy)

Custom Error Page

Create pages/_error.tsx:

API Routes

getServerSideProps / getStaticProps

Middleware

Instrument Next.js middleware:
Sentry automatically instruments middleware when using withSentryConfig.

Performance Monitoring

Automatic Instrumentation

The SDK automatically creates spans for:
  • Page loads
  • Navigation
  • API routes
  • Server components
  • Data fetching (fetch, getServerSideProps, etc.)

Custom Spans

Session Replay

Configure Session Replay in your client config:

Source Maps

The SDK automatically handles source map uploads when you use withSentryConfig. Configure upload options:

Webpack vs Turbopack

Next.js 16+ uses Turbopack by default. Next.js 15 and earlier use Webpack.

Turbopack Considerations

With Turbopack:
  • No build-time function wrapping
  • Automatic instrumentation via OpenTelemetry
  • Source maps handled differently
  • Some webpack-specific options don’t apply

Webpack Configuration

For Webpack builds, the SDK wraps your functions automatically. You can control this:

Advanced Configuration

Sampling

Environment Detection

Context

Troubleshooting

Errors Not Captured on Server

  1. Ensure sentry.server.config.ts exists and is configured
  2. Check that withSentryConfig is wrapping your Next.js config
  3. Verify environment variables are set correctly

Source Maps Not Uploaded

  1. Check SENTRY_AUTH_TOKEN is set
  2. Ensure org and project are correct in next.config.js
  3. Verify your Sentry auth token has the correct permissions

Build Errors with Turbopack

  1. Update to the latest @sentry/nextjs version
  2. Some webpack-specific options don’t work with Turbopack
  3. Check Next.js version compatibility
For Next.js 16+ with Turbopack, some configuration options related to automatic instrumentation are ignored since Turbopack doesn’t support build-time code transformation.

Next Steps