@sentry/sveltekit package provides comprehensive error tracking and performance monitoring for SvelteKit applications, with support for both client and server environments.
Prerequisites
- Node.js 18 or newer
- SvelteKit 2.0.0 or newer
- Vite 4.2 or newer (recommended)
- A Sentry account and project DSN
The SDK works best with Vite 4.2 and newer. Older versions might not generate source maps correctly.
Quick Installation with Wizard
The fastest way to get started is using the Sentry SvelteKit Wizard:- Install
@sentry/sveltekit - Create configuration files
- Set up source map uploads
- Configure hooks
Manual Installation
1
Install the Package
Install Current Version: 10.42.0
@sentry/sveltekit using your preferred package manager:2
Create Client Configuration
Create
src/hooks.client.ts (or .js):3
Create Server Configuration
Create
src/hooks.server.ts (or .js):4
Configure Vite
Update
vite.config.ts to include Sentry’s Vite plugin:5
Set Environment Variables
Add your Sentry auth token to
.env:6
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.
Error Handling
Automatic Error Capture
The SDK automatically captures:- Unhandled exceptions on both client and server
- Errors in load functions
- Errors in server endpoints
- Errors in form actions
Custom Error Handling
You can customize error handling:Load Functions
Errors in load functions are automatically captured:Performance Monitoring
Automatic Instrumentation
The SDK automatically creates spans for:- Page loads
- Navigation
- Server requests
- Load functions
- Form actions
Custom Spans
Server-Side Usage
Server Load Functions
Form Actions
API Routes
Client-Side Usage
In Svelte Components
Setting Context
Session Replay
Configure Session Replay in your client hooks:Adapter Compatibility
The Sentry SvelteKit SDK is compatible with most SvelteKit adapters:- ✅
@sveltejs/adapter-node - ✅
@sveltejs/adapter-vercel - ✅
@sveltejs/adapter-netlify - ✅
@sveltejs/adapter-cloudflare - ✅
@sveltejs/adapter-static - ✅
@sveltejs/adapter-auto
Some adapters may require additional configuration. Check the official documentation for adapter-specific setup.
Advanced Configuration
Environment Detection
Sampling
Custom Transport
Source Maps
Source maps are automatically handled by the Sentry Vite plugin when configured properly:Troubleshooting
Errors Not Captured
- Ensure both
hooks.client.tsandhooks.server.tsare configured - Check that
handleErroris exported from both files - Verify DSN is correct in both client and server configs
Source Maps Not Working
- Check
SENTRY_AUTH_TOKENis set correctly - Verify
organdprojectmatch your Sentry organization - Ensure Vite is generating source maps (check
vite.config.ts)
Performance Data Missing
- Ensure
tracesSampleRateis greater than 0 - Check that
sentryHandle()is in the hook sequence - Verify Sentry is initialized before the app runs
Build Errors
If you encounter build errors:- Update to the latest
@sentry/sveltekitversion - Ensure Vite is version 4.2 or newer
- Check SvelteKit version compatibility
Next Steps
- Configure Session Replay
- Set up Performance Monitoring
- Learn about Error Boundaries
- Explore Custom Instrumentation