@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:- Install
@sentry/nextjs - Create configuration files
- Set up source map uploads
- Add example error buttons (optional)
Manual Installation
1
Install the Package
Install Current Version: 10.42.0
@sentry/nextjs using your preferred package manager: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: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
Createpages/_error.tsx:
API Routes
getServerSideProps / getStaticProps
Middleware
Instrument Next.js middleware: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 usewithSentryConfig. 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
- Ensure
sentry.server.config.tsexists and is configured - Check that
withSentryConfigis wrapping your Next.js config - Verify environment variables are set correctly
Source Maps Not Uploaded
- Check
SENTRY_AUTH_TOKENis set - Ensure
organdprojectare correct innext.config.js - Verify your Sentry auth token has the correct permissions
Build Errors with Turbopack
- Update to the latest
@sentry/nextjsversion - Some webpack-specific options don’t work with Turbopack
- Check Next.js version compatibility
Next Steps
- Configure Error Boundaries
- Set up Session Replay
- Learn about Performance Monitoring
- Explore Server Components Monitoring